0

I use CreateProcess to start a .exe of mine. I want to know if everything worked fine or if it encouter errors when I tried to start this .exe.

From what I can tell, I need to use GetLastError(), but I tried to simulate an error in the process path but it return the same last error code.

So I want to catch if CreateProcess is successfull or not and if the process is done. What should I do to achieve that ?

Thanks.

gaalee
  • 105
  • 1
  • 7
  • 1
    Does this answer your question? [How do I call ::CreateProcess in c++ to launch a Windows executable?](https://stackoverflow.com/questions/42531/how-do-i-call-createprocess-in-c-to-launch-a-windows-executable) – harmonica141 Jul 19 '20 at 14:05
  • 2
    Check the return value of CreateProcess. True indicates success. – David Heffernan Jul 19 '20 at 15:19

2 Answers2

3

All the information you are looking for is spelled out in the documentation for CreateProcess:

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Note that the function returns before the process has finished initialization. If a required DLL cannot be located or fails to initialize, the process is terminated. To get the termination status of a process, call GetExitCodeProcess.

If you need to wait for the target process to terminate, call WaitForSingleObject on the process handle returned in the PROCESS_INFORMATION filled out by the call to CreateProcess.

Since you control the target process, you are free to choose any scheme that allows you to determine success or failure from the process' exit code. You can call GetExitCodeProcess at any time after the process' handle transitioned to the signaled state, and before you call CloseHandle on it.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
-1

There are several approaches I can think of:

#1 - Enumeration

You can enumarate the processes and check if the PID is in the list. Check out EnumProcesses

#2 - Checking Exit Codes

You can use GetExitCodeProcess. It will return STILL_ACTIVE (259) if the process is still running

#3 - Process Handles

WaitForSingleObject uses a Process handle with the SYNCHRONIZE access right and returns 0 if the process is not running.

Note: You should not specify INFINITE for the dwMilliseconds parameter because the function would not return until the process state became signaled(process is terminated).

0x45
  • 779
  • 3
  • 7
  • 26
  • The correct answer is of course to check the value returned by CreateProcess. – David Heffernan Jul 19 '20 at 15:19
  • 1
    Some processes return a 0 exit code even though they encounter an ‚error.‘, which results in the process not starting. I don’t think that’s the correct answer but it may work. An example I can think of is starting MEMU from shell, which always result in return code 0. **If the binary is programmed poorly you don’t get any useful return codes!** – 0x45 Jul 19 '20 at 17:20
  • 1
    The OP is starting *their own program*. They have all the tools at their disposal to signal success/failure through their process' exit code. – IInspectable Jul 19 '20 at 18:23