I'm trying to launch another process from a service (it's a console app that collects some data and writes it to the registry) but for some reason I can't get it to launch properly.
I basics of what I'm am trying to do is as follows:
- Launch the process
- Wait for the process to finish
- Retrieve the return code from the process
I am currently using the following code:
STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
if (CreateProcess(PATH, ARGS, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
::WaitForSingleObject(processInfo.hProcess, INFINITE);
DWORD exit = 100;
GetExitCodeProcess(processInfo.hProcess, &exit);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
return exit;
}
Upon calling CreateProcess(), it succeeds and enters the body of the if statement. The call to WaitForSingleObject returns immediately, which it should not, as the process should take approximately 20-30 seconds to finish. And, finally, calling GetExitCodeProcess() fails and does not set the value "exit".
FYI, this is code I have actually used elsewhere with success, just not in a service.
Could it be that it's being launched from a service and there are permissions issues??
Edit: I've now realized that it will actually launch the app (I can see it in TaskMan) but it seems to be stuck. It's there, but isn't doing anything.
Based on Rob Kennedy's suggestion, I fixed the process handle issue, and it actually does wait for the process to finish. But it never does finish unless I kill it manually.