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

时间:2019-01-18 00:50:05

标签: java winapi process 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;
    }
  }

}

1 个答案:

答案 0 :(得分:0)

感谢雷米·勒博(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;
    }
  }