Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging with "Command Arguments" in Visual Studio not working

VS not running WITH the "Command Arguments" I passed in. What am I not doing right?

Trying to work with "Command Arguments" in Visual Studio.
I broke it down to a simple little ConsoleApplication program in C.

#include "pch.h"
#include <iostream>

int main(int argc, char *argv[])
{
  printf("\nargv: ");
  for (int i = 0; i < argc; i++)
  {
    printf(" %s", argv[i]);
  }
  printf("\n");
}

I then set the "Command Arguments": Project: Properties: Debugging:"Command Arguments": mom

enter image description here

I hit the RUN button and this is displayed(my argument is not passed in):

argv:  C:\Users\jack\source\repos\ConsoleApplication45\Debug\ConsoleApplication45.exe

C:\Users\jack\source\repos\ConsoleApplication45\Debug\ConsoleApplication45.exe (process 1812) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

When I run from the command line I get (Works as I want it to with the parameter passed in: "mom"):

C:\Users\jack\source\repos\ConsoleApplication45\Debug>ConsoleApplication45.exe mom

argv:  ConsoleApplication45.exe mom

C:\Users\jack\source\repos\ConsoleApplication45\Debug>

Thank you for the link Jean-François Fabre:
Yes I had to change to x64 from x86.

enter image description here

like image 673
jdl Avatar asked Jan 14 '19 20:01

jdl


3 Answers

As explained in https://social.msdn.microsoft.com/Forums/vstudio/en-US/4097114c-8678-46bb-ba3b-7a2da8514efc/visual-studio-2017-not-passing-command-line-arguments-to-the-application?forum=vsdebug, you are setting the arguments only for x64 target when you need to set them for some other CPU.

It's possible that you're running x86 instead, where the arguments are empty.

The best method is to set them to "Any CPU" as it is very unlikely to be different depending on your target processor.

like image 196
Jean-François Fabre Avatar answered Sep 28 '22 09:09

Jean-François Fabre


I think your main problem here is a mismatch of the solution configuration/debugging properties vs. what Platform you are actually debugging/running the solution under.

Make sure these are aligned.

Example - this configuration is for Platform: x64

enter image description here

When the solution is ran/debugged, you need to make sure you are debugging in the same platform:

enter image description here

If your Platform is not x64,then configure and run the project under Platform: x86 or All Platforms as suggested.

like image 22
static_cast Avatar answered Sep 28 '22 09:09

static_cast


I thought I was seeing a similar issue, but realized the problem was with the operation of the Property Pages dialog. In previous versions of Visual Studio, selecting a project and right-clicking Properties would open up the dialog with the configuration set to the active configuration.

With Visual Studio 2017 this is no longer the case, and seems to open with the last edited configuration, which is confusing and could lead to someone editing the setting for the wrong configuration, as seen above. Not a good change...

like image 37
leona Avatar answered Sep 28 '22 11:09

leona