__argv[1]

Hi All:

I am using vc++.NET and have "12345" as a command line argument. I am trying to access this argument using my program. InitInstance function is shown below.

Please

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable
      
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, CW_USEDEFAULT, 310, 250, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   AfxMessageBox(__argv[1],MB_OK);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

I get __argv[0] correctly but do not get __argv[1]. Kindly suggest something here...

Thanks,
Chirag
LVL 1
cmandanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cmandanAuthor Commented:
Hello Experts:

I am able to display __argv[0] with the code below, but I get blank when I try to display __argv[1].......I am using vc++.NET

This is the code again,

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable
      
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, CW_USEDEFAULT, 310, 250, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   AfxMessageBox(__argv[0],MB_OK);
   ::MessageBox(0,__argv[1],"command1",MB_OK);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

so I get argv[0] OK, but I get a blank dialog box for argv[1]....waiting for your kind suggestions..

Thanks,
Chirag
AndyAinscowFreelance programmer / ConsultantCommented:
I have tried your code as the first line of an app.  I get the path of the exe as the first and then 1234 as the second message box.

Does yours still behave oddly with

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  AfxMessageBox(__argv[0],MB_OK);
   ::MessageBox(0,__argv[1],"command1",MB_OK);

   HWND hWnd;
....
AndyAinscowFreelance programmer / ConsultantCommented:
Another possibility is to check starting your app from a shortcut to make certain you are passing 1234 as a parameter.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

cmandanAuthor Commented:
Well, when i type the same code in vc++ 6.0, I get the value of __argv[1]. But I don't get it in vc++.NET.....pls guide. I know the code is right, but it could be the settings....

Thanks,
Chirag
cmandanAuthor Commented:
AfxMessageBox(__argv[0]);
AfxMessageBox(__argv[1]);

Output:
1st dialog box
"C:\test.exe"
2nd dialog box
blank

I checked Properties>debugging>Command Line arguments and it is 123.

I would really appreciate your suggestions here ASAP..

Thanks,
Chirag
AndyAinscowFreelance programmer / ConsultantCommented:
Is this a new improved feature of VC.Net to improve productivity?
Sorry, no further ideas.  The code looks OK and functions in an earlier VC++ version.
_nn_Commented:
Wondering if it wouldn't be a ansi/unicode problem. What happens if you replace __argv by __targv or __wargv ?
cmandanAuthor Commented:
I found out this problem which is very strange....when I try to execute the code from the .exe file after double clicking on it, it  gives me a blank argv[1] value, but when I click on start to debug the file, it gives me the __argv[1] value correctly. In the earlier version of vc++ 6.0, this was not the case, but I don't know if there are some special settings required in this new version.

--chirag
AndyAinscowFreelance programmer / ConsultantCommented:
when I try to execute the code from the .exe file after double clicking on it

If you double click on it it doesn't have any startup command line hence the behaviour qou experience.
You need a shortcut supplying the parameters.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cmandanAuthor Commented:
Great....got the explanation!! Thanks Andy, you get all the points...here you go!!

-Chirag
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.