Solved

__argv[1]

Posted on 2003-11-14
10
862 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:cmandan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 1

Author Comment

by:cmandan
ID: 9752094
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
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9753903
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;
....
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9753911
Another possibility is to check starting your app from a shortcut to make certain you are passing 1234 as a parameter.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:cmandan
ID: 9754886
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
0
 
LVL 1

Author Comment

by:cmandan
ID: 9758013
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
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9758260
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.
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9759132
Wondering if it wouldn't be a ansi/unicode problem. What happens if you replace __argv by __targv or __wargv ?
0
 
LVL 1

Author Comment

by:cmandan
ID: 9760197
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
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 50 total points
ID: 9761915
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.
0
 
LVL 1

Author Comment

by:cmandan
ID: 9762019
Great....got the explanation!! Thanks Andy, you get all the points...here you go!!

-Chirag
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 146
repeatEnd java challenge 42 85
Doc'in system (example?) BA 7 127
ShiftLeft challenge 21 95
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question