如何使用Win32 API获取(Java)进程的退出代码?

时间:2009-04-23 23:28:30

标签: windows winapi jvm status termination

如何从启动此JVM的Windows程序中获取JVM退出代码(来自call:System.exit(status)的'status'值)?我试图使用ShellExecute()调用的结果,但结果(42)独立于状态的实际值。

2 个答案:

答案 0 :(得分:2)

使用ShellExecuteEx而不是ShellExecute启动外部应用程序。

在调用ShellExecuteEx之前,在ShellExecuteEx函数的参数中启用SEE_MASK_NOCLOSEPROCESS标志。然后,您将在ShellExecuteEx函数的参数的hProcess字段中收到已启动进程的句柄。

ShellExecuteEx:http://msdn.microsoft.com/en-us/library/bb762154(VS.85).aspx

然后,使用WaitForSingleObject函数或任何其他WaitFor *函数等待外部应用程序终止。

WaitForSingleObject:http://msdn.microsoft.com/en-us/library/ms687032.aspx

然后,使用GetExitCodeProcess函数读取外部进程的退出代码。

GetExitCodeProcess:http://msdn.microsoft.com/en-us/library/ms683189(VS.85).aspx

答案 1 :(得分:1)

该函数的MSDN docs表明它不会从被调用的应用程序返回退出代码。

  

如果函数成功,则返回a   值大于32.如果是函数   失败,它返回一个错误值   表示失败的原因。   返回值被转换为   HINSTANCE用于向后兼容   使用16位Windows应用程序。它   然而,并不是真正的HINSTANCE。它   只能转换为int和   与32或以下相比   错误代码如下。

在MSDN上似乎也有getting exit status的示例代码。