| | |
Command-line argument syntax
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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
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 ...
So in WinMain do something like this:
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:
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!
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
lpCmdLine = GetCommandLine();
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)
#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!
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
•
•
Join Date: Jun 2005
Posts: 69
Reputation:
Solved Threads: 0
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");
}
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");}
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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++
•
•
Join Date: May 2007
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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)
if(CommandLineToArgvW(GetCommandLineW(), &argc)) printf("commandline: %s %d\n", GetCommandLine(), argc); //CommandLineToArgvW - get# argc else return 0;
![]() |
Similar Threads
- To fetch data through command line arguments (C++)
- What is the Command-Line Argument for??? (C++)
- command line arguments problem. (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: stack help
- Next Thread: how to download wxWidgets
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





