Hi, herewith a minimal example reproducing the problem with CreateProcess:
#include <windows.h>
#include <iostream>
int main()
{
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
char command[] = "cmd /C \"%ProgramFiles%\\Internet Explorer\\iexplore.exe\"";
if (!::CreateProcess(NULL, command, NULL, NULL, TRUE, 0, 0, NULL, &;;si, &;;pi))
{
std::cerr << "CreateProcess error " << ::GetLastError() << "\n";
return -1;
}
::WaitForSingleObject( pi.hProcess, INFINITE );
DWORD exitCode;
::GetExitCodeProcess(pi.hProcess, &;;exitCode);
std::cout << "Process finished with code " << exitCode << "\n";
return 0;
}
This works fine and IE starts. However if I replace 'C:\\Program Files' with '%ProgramFiles%, I receive the following error in the console:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Process finished with code 1
%ProgramFiles% env. variable is set correctly. It seems that whуn env variable is expanded, the quotes surrounding the command are just gone. Can anyone please explain me what happens?
BTW the remedy is:
1. replace quites with double quotes
2. or call ExpandEnvironmentStrings to prepare command for CreateProcess
But these seem workarounds to me. Whom shell I blame? Myself or CreateProcess?