Command Arguments field in Visual Studio 2015 - c++

I try to pass some arguments to the program using Visual Studio Community 2015.
I add some input in the Configuration Properties - Debugging - Command Arguments.
#include <iostream>
int main(int args, char* argv[])
{
std::cout << "args:" << args << std::endl;
for (size_t i = 0; i < args; i++)
{
std::cout << "argv[" << i << "]=" << argv[i] << std::endl;
}
}
The output is:
args:1
argv[0]=c:\users\john\documents\visual studio 2015\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe
Why the input in the "Command Arguments" field is ignored?

Are you sure the build configuration you set the command line for is the one you are running? VS allows different command line arguments to be set for each build (debug/release and platforms by default).

Related

unable to set command line parameters when debugging in visual studio code using cmake

I have set up a cmake project using visual studio 2019. I want to start the debug session with some command line parameters.
To achieve that, I've configured the CMakeLists.txt with the VS_DEBUGGER_COMMAND_ARGUMENTS:
set( VS_DEBUGGER_COMMAND_ARGUMENTS "this" "is" "a" "test" )
set( VS_DEBUGGER_WORKING_DIRECTORY "." )
The first thing my C++ code do is to print out that parameters:
if (argc > 1) {
// config_file = argv[1];
for (std::size_t i = 0; i < argc; i++) {
std::cout << "argument " << i << ": " << argv[i] << std::endl;
}
}
else {
std::cout << "the value of argc is: " << argc << std::endl;
}
The problem is that when I run Debug, the output I've always see is the value of argc is 1. I've also tried to modify the launch.vs.json file as appears in this related question:
Adding command line arguments to project
and it doesn't work. Any ideas?

How to handle Microsoft strange command line handling of escape character

A third party system calls a program I am writing that takes in a path as the first argument on the command line. An example command line might be something like:
"C:\Program Files (x86)\ProgramX"
Actually this isn't printed how I want in this editor, an example input might be:
"C:\Program Files (x86)\ProgramX\"
The problem is that Windows seems to correctly identify the first and second backslash as backslash characters and not the escape character. But the third backslash is seen as an escape of " and so you end up argv[1] being:
C:\Program Files (x86)\ProgramX"
Is this a bug? What is a reliable way to handle this issue?
Sample code below.
If you run the program: test.exe "C:\Program Files (x86)\ProgramX"
it prints:
>test.exe "C:\Program Files (x86)\ProgramX\"
No. args: 2
arg[0] = test.exe
arg[1] = C:\Program Files (x86)\ProgramX"
path: C:\Program Files (x86)\ProgramX" does not exist
/*
pass in for example
test.exe "C:\Program Files (x86)\ProgramX\"
prints path does not exist
*/
#include <string>
#include <iostream>
#include <windows.h>
bool DirectoryExists(const char* path)
{
DWORD dwAttrib = GetFileAttributes(path);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
int main(int argc, char* argv[]) {
std::cout << "No. args: " << argc << std::endl;
for (int i = 0; i < argc; i++) {
std::cout << "arg[" << i << "] = " << argv[i] << std::endl;
}
if (argc > 1) {
std::string path(argv[1]);
std::cout << "path: " << path << (DirectoryExists(path.c_str()) ? " exists" : " does not exist") << std::endl;
}
}

C++ 'speechapi_cxx.h': No such file or directory Visual Studio

I'm trying voice to text functions at Visual Studio 2019. I found this code on Microsoft website yet compiler says 'speechapi_cxx.h': No such file or directory.
........................................................................
#include <iostream> // cin, cout
#include <speechapi_cxx.h>
using namespace std;
using namespace Microsoft::CognitiveServices::Speech;
void recognizeSpeech() {
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
auto config = SpeechConfig::FromSubscription("YourSubscriptionKey", "YourServiceRegion");
// Creates a speech recognizer
auto recognizer = SpeechRecognizer::FromConfig(config);
cout << "Say something...\n";
// Starts speech recognition, and returns after a single utterance is recognized. The end of a
// single utterance is determined by listening for silence at the end or until a maximum of 15
// seconds of audio is processed. The task returns the recognition text as result.
// Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
// shot recognition like command or query.
// For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
auto result = recognizer->RecognizeOnceAsync().get();
// Checks result.
if (result->Reason == ResultReason::RecognizedSpeech) {
cout << "We recognized: " << result->Text << std::endl;
}
else if (result->Reason == ResultReason::NoMatch) {
cout << "NOMATCH: Speech could not be recognized." << std::endl;
}
else if (result->Reason == ResultReason::Canceled) {
auto cancellation = CancellationDetails::FromResult(result);
cout << "CANCELED: Reason=" << (int)cancellation->Reason << std::endl;
if (cancellation->Reason == CancellationReason::Error) {
cout << "CANCELED: ErrorCode= " << (int)cancellation->ErrorCode << std::endl;
cout << "CANCELED: ErrorDetails=" << cancellation->ErrorDetails << std::endl;
cout << "CANCELED: Did you update the subscription info?" << std::endl;
}
}
}
int main(int argc, char** argv) {
setlocale(LC_ALL, "");
recognizeSpeech();
return 0;
}
Speech SDK is not installed on your machine. You may download it here https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/speech-sdk

displaying the output in Ouput windows in visual studio 2015

I'm debugging the c++ console application in visual studio 2015 but output is not displaying in debug output window.
std::ostringstream buffer;
std::cout << "\n result: \n";
for (int i = 0; i < size; i++)
{
buffer << array[i] << ",";
}
buffer << "\n";
In the output there are some warnings Cannot find or open the PDB file but it may not be reason.
What is the problem ?
To display in MSVC debug output you need to use ::OutputDebugString().
eg ::OutputDebugString( buffer.str() );

Issues with C++ command-line arguments on Windows

I'm somehow having issues parsing command-line arguments on Windows in C++.
I tried using this
int main(int argc, char **argv)
{
std::cout << "Command-line argument count: " << argc << " \n";
std::cout << "Arguments:\n";
for (int i = 0; i < argc; i++)
std::cout << " argv[" << i << "] "
<< argv[i] << "\n";
return 0;
}
as well as this
int main(int argc, char *argv[])
{
std::cout << "Command-line argument count: " << argc << " \n";
std::cout << "Arguments:\n";
for (int i = 0; i < argc; i++)
std::cout << " argv[" << i << "] "
<< argv[i] << "\n";
return 0;
}
The variables argc and argv seem to be somehow uninitialized.
That's what launching the program returns to me:
Z:\Dev\ProcessSuspender\Debug>ProcessSuspender a
Command-line argument count: 2130558976
Arguments:
argv[0]
argv[1] ╠ÉÉÉÉÉj↑h╚♂YwÞØ÷■ âe³
argv[2]
(crash following)
I compiled it with MSVC12 using the /SUBSYSTEM:CONSOLE linker option.
What could be the cause of this issue?
I've manually set the entry point to main. Whether I use the default project setting (_tmain) or not, the issue persists.
In general, you should not do that unless you know the consequences. The typical values of the entry point (/ENTRY) should be either:
[w]mainCRTStartup, which calls [w]main, or
[w]WinMainCRTStartup, which calls [w]WinMain, or
_DllMainCRTStartup, which calls DllMain.
Why is this needed? Well, the …CRTStartup-family of functions do a couple crucial things, including the initialization of:
the C runtime (CRT),
any global variables, and
the arguments argc and argv, as you've accidentally found out.
So for a typical program you probably want it to do its job. In the Linux world, there is an equivalent function called _start that is needed to do the same initialization tasks as well, which can be overridden with -e while linking.
The confusion here probably stems from difference in ambiguous meaning of the word "entry point": there is the meaning of "apparent entry point" from the perspective of the language (which is main and its ilk), and the meaning of the "true entry point" from the perspective of the language implementation (which is …CRTStartup or _start).
Note that using the …CRTStartup functions is not absolutely essential, as you can certainly write a program that avoids using them. It does come with a cost, however:
you can't use the C runtime, so you can't use most of the standard library,
you need to manually initialize any global variables, and
you need to manually obtain argc and argv using the Windows API (GetCommandLineW and CommandLineToArgvW).
Some do this to avoid dependency on the CRT, or to minimize the executable size.
I tried your project on VS 2012 and it is working smoothly.
I added a getchar(); command as below:
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Command-line argument count: " << argc << " \n";
std::cout << "Arguments:\n";
for (int i = 0; i < argc; i++)
std::cout << " argv[" << i << "] "
<< argv[i] << "\n";
getchar();
return 0;
}
so that i could see the output.
Right-click on Project -> Properties -> debugging -> Command
Arguments.
This was empty in my project and i added the character a to simulate your problem.
Here is the output i am getting:
Right click on the project -> Debug -> Start new Instance -> would you
like to build it -> yes
Output:
Command-line argument count: 2
Arguments:
argv[0] <my macines path>\helpingStack1.exe
argv[1] a
Please check this again. I hope this helps.
1) I am suspecting that your binaries are not up to date when you run this script so please do a clean build and verify that you are indeed running the same exe as the one you are building. Please check the configuration - Debug/Release.
2) go to the folder where you have created the project and righclick on the project folder, and change property -> ensure read only is not checked in the check box.
Obviously, Something is wrong with the IDE or project or maybe anything else's setup on your system only.
The code is perfect.
Have you tried directly and independently running your output exe, by executing it through command prompt ??
Run your exe with command prompt by supplying some arbitrary arguments, and check the output.
its worth to check your character set in project properties->General.