【问题标题】:Java JNA using kernal32 VirtualQueryEx returns zero (no success error code)使用 kernal32 VirtualQueryEx 的 Java JNA 返回零(无成功错误代码)
【发布时间】:2015-04-30 20:56:52
【问题描述】:

我正在尝试使用来自 kernal32.dll Windows API 函数的 VirtualQueryEx。

我在调用这个函数之前得到的所有指针/地址都是正确的。

对 VirtualQueryEx 的调用返回 0 ,表示没有成功。

GetLastError() 也返回错误代码 5,表示访问被拒绝):

请问我做错了什么?

  • Windows 8,管理员权限。

JNA 映射:

public class Test
{
    static Kernel32   kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
    static User32     user32 = (User32)   Native.loadLibrary("user32"  , User32.class);

    public static  void main(String[] args)
    {
        int pid = getProcessId("someWindowName"); // get our process ID

        Pointer readprocess = kernel32.OpenProcess(0x0010, false,pid); // open the process ID with read priviledges.

        MEMORY_BASIC_INFORMATION l = new MEMORY_BASIC_INFORMATION();

        SYSTEM_INFO info =  new SYSTEM_INFO();

        kernel32.GetSystemInfo(info);          

        System.out.println(kernel32.VirtualQueryEx(readprocess, info.lpMinimumApplicationAddress, l, l.size()));
        System.out.println(kernel32.GetLastError());

    }

    public static int getProcessId(String window)
    {
        IntByReference pid = new IntByReference(0);
        user32.GetWindowThreadProcessId(user32.FindWindowA(null,window), pid);

        return pid.getValue();
    }

    public static Pointer openProcess(int permissions, int pid)
    {
        Pointer process = kernel32.OpenProcess(permissions,true, pid);
        return process;
    }

    public static Memory readMemory(Pointer process, int address, int bytesToRead)
    {
        IntByReference read = new IntByReference(0);
        Memory output = new Memory(bytesToRead);

        kernel32.ReadProcessMemory(process, address, output, bytesToRead, read);
        return output;
    }
}

kernal32 内部

    int VirtualQueryEx(Pointer readprocess, Pointer lpMinimumApplicationAddress,MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);

memory_basic 结构:

public  class MEMORY_BASIC_INFORMATION extends Structure {


    public Pointer baseAddress;

    public Pointer allocationBase;

    public NativeLong allocationProtect;

    public SIZE_T regionSize;

    public NativeLong state;

    public NativeLong protect;

    public NativeLong type;

}

谢谢!

【问题讨论】:

    标签: java jna windows-api-code-pack


    【解决方案1】:

    您必须从MSDN 获取进程句柄PROCESS_QUERY_INFORMATION,其值为0x0400。您正在使用 0x0010 打开进程,因此出现“拒绝访问”错误。

    句柄必须已使用 PROCESS_QUERY_INFORMATION 打开 访问权限,允许使用句柄从 进程对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 2021-09-18
      • 1970-01-01
      • 2022-10-15
      • 2019-03-24
      • 2020-02-15
      相关资源
      最近更新 更多