Command-line argument syntax

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 69
Reputation: DotNetUser is an unknown quantity at this point 
Solved Threads: 0
DotNetUser DotNetUser is offline Offline
Junior Poster in Training

Command-line argument syntax

 
0
  #1
Sep 29th, 2005
I'm using Visual C++.NET. I need to pass a port number to my gui. What the syntax for passing arguments to "int __stdcall WinMain()." For regular C++ it would be "int main(int argc, char* argv[])" Thanks.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Command-line argument syntax

 
0
  #2
Sep 29th, 2005
look up in your helpfiles or at msdn....

the third param to WinMain to get command line as ascii
GetCommandLine() to get the command line as unicode
CommandLineToArgvW() to convert it to an array of strings if necessary
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 7
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: Command-line argument syntax

 
0
  #3
Sep 30th, 2005
Straight from MSDN.com ...

int WinMain(

HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
);

lpCmdLine [in] Pointer to a null-terminated string specifying the command line for the application, excluding the program name. To retrieve the entire command line, use the GetCommandLine function.
So in WinMain do something like this:
  1. lpCmdLine = GetCommandLine();
Then parse the lpCmdLine string. (REMEMBER IT DOES NOT INCLUDE THE PROGRAM PATH).

Also, a Visual C++ dependent way is to use __argv and __argc as you would argv and argc. This only applies to 32-bit windows programs. 16-bit windows programs require you to declare the variables the following way:

  1. #ifdef __cplusplus
  2. extern "C"
  3. #endif
  4. extern char ** __argv;

Just like argv, __argv includes the program name as the first element in the array.

So instead of parsing a string using CommandLineToArgvW() or CommandLineToArgvW() you can just access __argv directly.

Hope this helps!
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 69
Reputation: DotNetUser is an unknown quantity at this point 
Solved Threads: 0
DotNetUser DotNetUser is offline Offline
Junior Poster in Training

Re: Command-line argument syntax

 
0
  #4
Sep 30th, 2005
I get a compiler error :
error C2065 : 'HINSTANCE': undeclared identifier
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Command-line argument syntax

 
0
  #5
Sep 30th, 2005
#include<windows.h>??
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 69
Reputation: DotNetUser is an unknown quantity at this point 
Solved Threads: 0
DotNetUser DotNetUser is offline Offline
Junior Poster in Training

Re: Command-line argument syntax

 
0
  #6
Oct 4th, 2005
Below is a simple example I found on MSDN. I added the #include <windows.h> but it still has an error. Am I forgetting to set something in my environment? I'm using Visual C++.NET 2003.

error C2731: 'WinMain' : function cannot be overloaded

#include <windows.h>
#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>

#undef MessageBox // undefine the MessageBox macro of windows.h to use the managed MessageBox class

void __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
long lpCmdLine,
int nCmdShow)
{
System::Windows::Forms::MessageBox:how("Hello, Windows Forms");
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: Command-line argument syntax

 
0
  #7
Oct 4th, 2005
you appear to be mixing managed and unmanaged c++. I dont think you can do that although I aint sure i dont use managed c++
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 69
Reputation: DotNetUser is an unknown quantity at this point 
Solved Threads: 0
DotNetUser DotNetUser is offline Offline
Junior Poster in Training

Re: Command-line argument syntax

 
0
  #8
Oct 5th, 2005
I added stdafx.h/.cpp and now it's working. But, how do you detect that no arguments were passed in? I want to set a default value when arguments are not provided.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1
Reputation: cpg is an unknown quantity at this point 
Solved Threads: 0
cpg cpg is offline Offline
Newbie Poster

Re: Command-line argument syntax

 
0
  #9
May 4th, 2007
Originally Posted by DotNetUser View Post
I added stdafx.h/.cpp and now it's working. But, how do you detect that no arguments were passed in? I want to set a default value when arguments are not provided.


  1. if(CommandLineToArgvW(GetCommandLineW(), &argc))
  2. printf("commandline: %s %d\n", GetCommandLine(), argc); //CommandLineToArgvW - get# argc
  3. else
  4. return 0;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC