There is a program "prog.exe". It can be run from the command line:
C:\Folder\prog.exe "D:\3.7.7\File.XML"
. I'm trying to do the same thing using CreateProcess
. Program froze up, threw modal window with an error and terminated by timeout.
I tried to run using ShellExecuteEx
. Program worked and finished correctly. But i can't use ShellExecuteEx
because of problems with file association( error code 1155 ).
There are 2 questions:
1. Why program can be run from the command line, with ShellExecuteEx
it also works, but with CreateProcess
it doesn't work? Am I doing something wrong maybe?
2. Why program modal window is displayed? Why not hidden? Why are flags ignored:
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
But when i run another program, these flags work and hide GUI.
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
std::string cmd_line = "C:\\Folder\\prog.exe \"D:\\3.7.7\\File.XML\"";
const char *ss = (cmd_line.c_str());
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
LPSTR lp_str = const_cast<char *> (ss);
if (!CreateProcess(NULL, lp_str, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))
{
std::ofstream fs("D:\\3.7.7\\log.txt");
fs << " Can't run prog.exe! ErrorCode: " << GetLastError();
fs.close();
}
std::ofstream fs("D:\\3.7.7\\log.txt");
fs << "Run correctly \n";
switch (WaitForSingleObject(pi.hProcess, 20000))
{
case WAIT_OBJECT_0:
return;
case WAIT_TIMEOUT:
TerminateProcess(pi.hProcess, 0);
CloseHandle(pi.hProcess);
fs << " Waiting to long. Terminate process ";
break;
case WAIT_FAILED:
fs << " Waiting failed. ErrorCode: " << GetLastError();
break;
}
fs.close();