window 进程通讯java_如何用JNA确定Java中Windows进程的位?

我正在编写一个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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值