0

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(); 
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 3
    You should show actual code rather than this fake code. Then we could see what you actually pass to `CreateProcess` rather than having to guess at what `ss` is. Your use of `const_cast` should be a great big warning sign to you. If something is not `const`, then applying `const_cast` does not make it so! You might get away with this with `CreateProcessA` due to a quirk of implementation, but it's a very bad habit to form. Furthermore, if you encounter an error, you have to tell us the details of that error. Make sure we have as much information as you do, and the same code as you. – David Heffernan Dec 03 '15 at 11:27
  • Does the behavior of the program being run depend on the working directory? You can try C++ statement `__debugbreak();` in the beginning of the program being run, so to debug it and see what's wrong after it's run using `CreateProcess`. – Serge Rogatch Dec 03 '15 at 16:17
  • I think you want it to run a console program in hidden mode, using `CreateProcess`. See this question http://stackoverflow.com/questions/780465/winapi-createprocess-but-hide-the-process-window (see `CREATE_NO_WINDOW`) – Barmak Shemirani Dec 03 '15 at 16:32
  • What is the actual error that `CreateProcess()` is reporting? What does the modal window actually say/look like? When `CreateProcess()` fails, you are calling `GetLastError()` too late. You must call it before doing anything else. But you are opening a file before calling `GetLastError()`, so the act of opening that file can wipe out the error code that was set by `CreateProcess()`. Also, your code is still proceeding with its success logic (logging "Run correctly" and waiting on `hProcess `) if `CreateProcess()` fails, which is clearly wrong. – Remy Lebeau Dec 03 '15 at 18:39

0 Answers0