如何检查 CreateProcess 是否正常工作或 运行?
How can I check if CreateProcess is working or running?
我用CreateProcess
启动我的.exe。我想知道是否一切正常,或者当我尝试启动此 .exe 时是否遇到错误。
据我所知,我需要使用 GetLastError()
,但我试图模拟进程路径中的错误,但它 return 与上一个错误代码相同。
所以我想知道 CreateProcess
是否成功以及过程是否完成。我应该怎么做才能做到这一点?
谢谢。
我能想到的几种方法:
#1 - 枚举
您可以枚举进程并检查 PID
是否在列表中。查看 EnumProcesses
#2 - 检查退出代码
您可以使用 GetExitCodeProcess。它会 return STILL_ACTIVE
(259) 如果进程仍然 运行
#3 - 进程句柄
WaitForSingleObject 使用具有 SYNCHRONIZE 访问权限的进程句柄,如果进程不是 运行.
,则 returns 0
注意:您不应为 dwMilliseconds 参数指定 INFINITE,因为该函数不会 return 直到进程状态变为有信号(进程终止)。
您要查找的所有信息都在 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.
如果需要等待目标进程终止,调用WaitForSingleObject on the process handle returned in the PROCESS_INFORMATION 由调用CreateProcess
填写。
由于您控制了目标进程,因此您可以自由选择任何允许您从进程的退出代码确定成功或失败的方案。您可以在进程的句柄转换为信号状态之后和对其调用 CloseHandle
之前随时调用 GetExitCodeProcess
。
我用CreateProcess
启动我的.exe。我想知道是否一切正常,或者当我尝试启动此 .exe 时是否遇到错误。
据我所知,我需要使用 GetLastError()
,但我试图模拟进程路径中的错误,但它 return 与上一个错误代码相同。
所以我想知道 CreateProcess
是否成功以及过程是否完成。我应该怎么做才能做到这一点?
谢谢。
我能想到的几种方法:
#1 - 枚举
您可以枚举进程并检查 PID
是否在列表中。查看 EnumProcesses
#2 - 检查退出代码
您可以使用 GetExitCodeProcess。它会 return STILL_ACTIVE
(259) 如果进程仍然 运行
#3 - 进程句柄
WaitForSingleObject 使用具有 SYNCHRONIZE 访问权限的进程句柄,如果进程不是 运行.
,则 returns 0注意:您不应为 dwMilliseconds 参数指定 INFINITE,因为该函数不会 return 直到进程状态变为有信号(进程终止)。
您要查找的所有信息都在 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.
如果需要等待目标进程终止,调用WaitForSingleObject on the process handle returned in the PROCESS_INFORMATION 由调用CreateProcess
填写。
由于您控制了目标进程,因此您可以自由选择任何允许您从进程的退出代码确定成功或失败的方案。您可以在进程的句柄转换为信号状态之后和对其调用 CloseHandle
之前随时调用 GetExitCodeProcess
。