我正在编写一个Java方法来确定由PID标识的指定Windows进程的位。代码通过JNA调用Win32 API函数。我的尝试如下,但即使给定64位进程的PID,它也始终返回32。代码路径始终相同(请参见代码段中的注释)。
我不确定这个方法在概念上是否有缺陷,或者在实现中是否有bug。
代码运行在64位Windows7上,使用32位JRE。我做错什么了?
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinBase.SYSTEM_INFO;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
public class Test
{
public static void main(String[] args)
{
int pid;
pid = 10340;
System.out.println(pid + " bitness = " + getProcessBitness(pid));
pid = 15116;
System.out.println(pid + " bitness = " + getProcessBitness(pid));
}
/**
* Given a process ID, determine the bitness of the process.
*
* @param pid
* @return 32 or 64
*/
public static int getProcessBitness(int pid)
{
Kernel32 kernel32 = Kernel32.INSTANCE;
IntByReference ref = new IntByReference();
WinNT.HANDLE hProcess = kernel32.OpenProcess(WinNT.PROCESS_TERMINATE, false, pid);
// See https://docs.microsoft.com/en-us/windows/desktop/api/wow64apiset/nf-wow64apiset-iswow64process
kernel32.IsWow64Process(hProcess, ref);
boolean isWow64 = (ref.getValue() == 1);
if (isWow64)
// WOW64 is the x86 emulator that allows 32-bit Windows-based applications to run seamlessly on 64-bit Windows
return 32; // This never happens
else
{
// The process bitness matches the OS bitness
int osBitness;
// See https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getnativesysteminfo
SYSTEM_INFO systemInfo = new SYSTEM_INFO();
kernel32.GetNativeSystemInfo(systemInfo);
if (systemInfo.processorArchitecture.pi.wProcessorArchitecture.intValue() == 0)
osBitness = 32; // This code path is always taken for both 32 and 64 bit processes
else
osBitness = 64; // This never happens
return osBitness;
}
}
}