I have some code that starts up an executable with system(). It works fine except that it shows up a command prompt for a little while.
We want to get rid of that, so I changed the code to use CreateProcess. Now no window shows up but the program crashes. Anyone know what Im doing wrong?
Code:
int doCmdLineCall(std::string command) const
{
//return system(command.c_str());
// mimic return value of system() to not change error handling behaviour
STARTUPINFO info = {0};
PROCESS_INFORMATION procInfo = {0};
BOOL ret = FALSE;
std::string::size_type i = command.find_first_of(' ');
std::string cmd;
std::string param;
if (i != std::string::npos)
{
cmd = command.substr(0, i);
i++;
param = command.substr(i, command.length());
info.cb = sizeof(info);
info.dwFlags = STARTF_USESHOWWINDOW;
info.wShowWindow = SW_HIDE;
ret = CreateProcessA(cmd.c_str(), (char *)param.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &info, &procInfo);
if (ret)
{
WaitForSingleObject(procInfo.hProcess, (DWORD)-1L);
DWORD exitcode;
GetExitCodeProcess(procInfo.hProcess, &exitcode);
ret = (intptr_t)(int)exitcode;
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
}
}
return 0;// system(command.c_str());
}