I'm writing a Java method to determine the bitness of a specified Windows process identified by PID. The code calls Win32 API functions through JNA. My attempt is below, but it always returns 32 even when given the PID of a 64 bit process. The code path is always the same (see the comments in the code snippet).
I'm not sure if the approach is conceptually flawed or if there is a bug in the implementation.
The code is running on 64-bit Windows 7 with a 32-bit JRE. What am I doing wrong?
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;
}
}
}
Thanks to Remy Lebeau, this is the corrected code:
/**
* 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_QUERY_LIMITED_INFORMATION, 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;
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);
int processorArchitecture = systemInfo.processorArchitecture.dwOemID.intValue() & 0xFFFF;
if (processorArchitecture == 0) // PROCESSOR_ARCHITECTURE_INTEL = x86
osBitness = 32;
else
osBitness = 64;
return osBitness;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.