This post describes the Windows API that can be used to get the command line and the full process name.
To get command line, use the function GetCommandLine. Of course we could use the arguments of the functions main (in case of console app) or WinMain (in case of GUI app) instead, but this code makes things a bit easier (for instance, in cases when we need to add command line parsing to large existing project or when we just want to print command line for debug purposes etc).
To get the path of the executable file of the current process, use GetModuleFileName.
So the code may look like:
char s[MAX_PATH];
printf("GetCommandLine=%s\n", GetCommandLine());
if (GetModuleFileName(NULL, s, sizeof(s) / sizeof(*s)) > 0)
printf("GetModuleFileName=%s\n", s);
To illustrate the difference between these two functions let's compile the program to "c:\t\testtesttest.exe" and run several tests from the command line:
c:\t>testtesttest.exe 1 2 3
GetCommandLine=testtesttest.exe 1 2 3
GetModuleFileName=c:\t\testtesttest.exe
c:\t>testte~1.exe 1
GetCommandLine=testte~1.exe 1
GetModuleFileName=c:\t\testtesttest.exe
c:\t>testtesttest arg1
GetCommandLine=testtesttest arg1
GetModuleFileName=c:\t\testtesttest.exe
c:\t>c:\t\testtesttest.exe
GetCommandLine=c:\t\testtesttest.exe
GetModuleFileName=c:\t\testtesttest.exe
No comments:
Post a Comment