If you see the samples from MSDN, or some other sources which is using CreateProcess API, you might have noticed it’s not using the first parameter (pszApplicationName) to specify the application to launch. Instead, the information required to create the process is being specified in the second parameter pszCommandLine. Why it’s like that? Let’s take the following example which is used to create the process.
STARTUPINFO stInfo= {sizeof( stInfo )};
PROCESS_INFORMATION stPInfo;
TCHAR cmdLine[] = TEXT( "NOTEPAD");
if( CreateProcess( NULL, cmdLine, NULL, NULL, FALSE, NULL, NULL,
NULL,&stInfo, &stPInfo ))
{
_tprintf( TEXT( "Successfully Launched %s"), cmdLine );
}
Let’s dig into the working of command line parameter. By using the above code, you can spawn notepad application. If the first parameter (pszApplicationName) is NULL, CreateProcess API behaves as follows. First it examines and assumes that the first token in the string is the desired process to launch. If the executable file doesn’t have an extension, .exe extension will be applied as default. API will search for the executable file in the following order.
1. If the file name contains full path, it will try to load from the specified path.
2. Directory containing the .exe file of the calling process.
3. Current directory of the calling process ( Current directory can be different from file location)
4. System directory and Windows directory
5. Directories listed in PATH environment variable.
If you’re passing the application name in the first parameter (pszApplicationName) it should definitely contains the extension. It will not put an .exe extension as default. If the path specified is not a fully qualified path, the API will search only in the current directory but not in any other directories as specified earlier.
STARTUPINFO stInfo= {sizeof( stInfo )};
PROCESS_INFORMATION stPInfo;
TCHAR cmdLine[] = TEXT( "WORDPAD Readme.txt");
if( CreateProcess( TEXT( "C:\\Windows\\Notepad.exe" ), cmdLine, NULL, NULL, FALSE, NULL, NULL,
NULL,&stInfo, &stPInfo ))
{
_tprintf( TEXT( "Successfully Launched %s"), cmdLine );
}
What if you specify valid strings in both Application Name and Command line parameters? The application will be launched with the specified exe name in the first parameter and the string in the command line parameters are passed as command line strings for the application. Note that, argv[0] doesn’t contains the application name in the above scenario.
Command line arguments will be argv[0] = “WORDPAD” and argv[1] = “Readme.txt”
The above code actually launches the ReadMe.txt file in Notepad.exe but if you pass the same parameters to your own application, it won’t load “ReadMe.txt” in Notepad. This means that, it’s purely application’s decision to interpret command line arguments. In the case of Notepad, it treats argv[0] as application’s name and the input for filename will be taken from Argv[1] if available. As per the POSIX standard, argv[0] should be equal to the application name. Most of the Windows Applications follows this behavior. (If you remove “WORDPAD” from the cmdLine string, “ReadMe.txt” will not be loaded).
Actually the purpose of Application Name parameter in CreateProcess API is to make it POSIX complaint. The POSIX standard for command line is having following format. Application Name, arg1, arg2, etc. (argv[0] contains application name)
Alternatively, you can pass the parameters to command line arguments as follows to meet the POSIX specification.
CreateProcess( _T( "c:\\MyApp.exe Param1 Param2"), NULL, ... );

Hey, nice tips. Perhaps I’ll buy a bottle of beer to the man from that forum who told me to go to your site