繁体   English   中英

如何使用JNA确定Java中Windows进程的位数?

[英]How to determine Windows process bitness in Java with JNA?

我正在编写Java方法来确定PID标识的指定Windows进程的位数。 该代码通过JNA调用Win32 API函数。 我的尝试在下面,但是即使给出64位进程的PID,它也总是返回32。 代码路径始终相同(请参见代码段中的注释)。

我不确定该方法在概念上是否存在缺陷或实现中是否存在错误。

该代码在具有32位JRE的64位Windows 7上运行。 我究竟做错了什么?

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;
    }
  }

}

感谢Remy Lebeau,这是已更正的代码:

  /**
   * 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;
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM