JNA有一个很奇怪的问题。 我正在使用GetExitCodeProcess()检查进程是否存在。因此,例如,我知道记事本是PID 2084.当我使用我的方法来检查PID 2084是否存在时,它在2084和2087之间的PID中返回true(尽管我完全确定PID 2085-2087不会“ t存在)。它对于其他PID(如2083和2088)返回false。JNA GetExitCodeProcess()奇怪地工作
这几乎就好像存在某种不可能的舍入误差,而OpenProcess()
正在打开一个不存在的PID句柄!
这发生在所有进程中。如果我列举所有进程并调用isRunning(PID),则当存在PID + 1,2 or 3
时,它将返回true。否则它返回false,所以至少它是部分工作的。
的图案总是一样的,它返回PID和PID + 3.
实施例输出之间真:
[Notepad PID = 2084, cmd.exe PID = 2100]
isRunning(2083)=False
isRunning(2084)=true
isRunning(2085)=true
isRunning(2086)=true
isRunning(2087)=true
isRunning(2088)=false
.... false .....
isRunning(2100)=true
等。
Interface code:
protected interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class);
public Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, int dwProcessId);
int GetLastError();
boolean GetExitCodeProcess(Pointer hProcess, IntByReference lpExitCode);
};
Function code:
public static boolean isRunning(int pid)
{
final int PROCESS_QUERY_INFORMATION = 0x0400;
final int STILL_ALIVE = 259;
final int INVALID_PARAM = 87;
Pointer hProcess = kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, false, pid);
int error = kernel32.GetLastError();
if (error == INVALID_PARAM)
return false; //Invalid parameter.
IntByReference exitCode = new IntByReference();
kernel32.GetExitCodeProcess(hProcess, exitCode);
if (exitCode.getValue() != STILL_ALIVE)
return false;
else
return true;
}
public static void main(String[] args) {
System.out.println(isRunning(2083)); //Proceses with PID 2083, 2085 to 2088 do not exist.
System.out.println(isRunning(2084)); //2084 is notepad
System.out.println(isRunning(2085));
System.out.println(isRunning(2086));
System.out.println(isRunning(2087));
System.out.println(isRunning(2088));
}
谢谢你的有用的文章。你有什么建议,我保留原来的方法,还是有什么我可以做的?不幸的是,如果我进入cmd并键入taskkill/PID 2084,它会告诉我该进程不存在。 – David
你的问题是关于为什么PID + 1,PID + 2和PID + 3都指向相同的过程。我回答说。现在你似乎在提出另一个问题。 'taskkill/pid xxxx'对我来说工作正常。如果你想调试这个问题,这可能是一个不同的问题。事实上,我现在看到你实际上没有提出直接的问题。我只是解释了你描述的行为。 –
啊不,我的意思是: Taskkill/pid作品。 Taskkill/pid + 3 Taskkill/pid + 2和Taskkill/pid + 1不起作用:( – David