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.

Recommended Answers

All 8 Replies

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

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:

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:

#ifdef __cplusplus
extern "C"
#endif
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!

I get a compiler error :
error C2065 : 'HINSTANCE': undeclared identifier

#include<windows.h>??

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::Show("Hello, Windows Forms");
}

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++

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.

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.

if(CommandLineToArgvW(GetCommandLineW(), &argc))
        printf("commandline: %s %d\n", GetCommandLine(), argc); //CommandLineToArgvW - get# argc
    else
        return 0;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.