943,846 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 20946
  • C++ RSS
Sep 29th, 2005
0

Command-line argument syntax

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
DotNetUser is offline Offline
69 posts
since Jun 2005
Sep 29th, 2005
0

Re: Command-line argument syntax

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
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 30th, 2005
0

Re: Command-line argument syntax

Straight from MSDN.com ...

Quote ...
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:
C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Sep 30th, 2005
0

Re: Command-line argument syntax

I get a compiler error :
error C2065 : 'HINSTANCE': undeclared identifier
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
DotNetUser is offline Offline
69 posts
since Jun 2005
Sep 30th, 2005
0

Re: Command-line argument syntax

#include<windows.h>??
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Oct 4th, 2005
0

Re: Command-line argument syntax

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");
}
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
DotNetUser is offline Offline
69 posts
since Jun 2005
Oct 4th, 2005
0

Re: Command-line argument syntax

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++
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Oct 5th, 2005
0

Re: Command-line argument syntax

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
DotNetUser is offline Offline
69 posts
since Jun 2005
May 4th, 2007
0

Re: Command-line argument syntax

Click to Expand / Collapse  Quote originally posted by DotNetUser ...
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.


C++ Syntax (Toggle Plain Text)
  1. if(CommandLineToArgvW(GetCommandLineW(), &argc))
  2. printf("commandline: %s %d\n", GetCommandLine(), argc); //CommandLineToArgvW - get# argc
  3. else
  4. return 0;
cpg
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpg is offline Offline
1 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: stack help
Next Thread in C++ Forum Timeline: how to download wxWidgets





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC