计算机专用jna.Native无效的内存访问

时间:2016-03-16 00:43:12

标签: java jna

我一直在开发一种能够自动跟踪某个程序的内存值的工具。经过一些研究,我已经在我自己的开发环境/我自己的计算机上成功完成了这项工作。但是,当我把这个节目提供给朋友时,他已经

java.lang.Error: Invalid memory access
    at com.sun.jna.Native.invokeInt(Native Method)
    at com.sun.jna.Function.invoke(Function.java:378)
    at com.sun.jna.Function.invoke(Function.java:323)
    at com.sun.jna.Library$Handler.invoke(Library.java:236)
    at com.sun.proxy.$Proxy0.ReadProcessMemory(Unknown Source)
    at main.sys.MemoryExtractor.readMemory(MemoryExtractor.java:56)

我已经问过各种各样的环境问题,但似乎没有任何相关内容。

以下是相关代码:

public static int PROCESS_VM_OPERATION = 0x0008;
public static int PROCESS_VM_READ = 0x0010;
public static int PROCESS_VM_WRITE = 0x0020;

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

public static int pid = 0;
public static Pointer process;

public static void init() {
    while (true) {
        System.out.println("Looking for Hockey? process");
        pid = getProcessId("Hockey?");
        if (pid == 0) {
            System.out.println("Hockey? process not found, press enter to try again");
            try {
                System.in.read();
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Hockey? process found");
            break;
        }
    }
    process = openProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, pid);
}

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(long address, int bytesToRead) {
    IntByReference read = new IntByReference(0);
    Memory output = new Memory(bytesToRead);
    kernel32.ReadProcessMemory(process, address, output, bytesToRead, read);
    return output;
}
public interface Kernel32 extends StdCallLibrary {
    boolean WriteProcessMemory(Pointer p, long address, Pointer buffer, int size,
        IntByReference written);

    boolean ReadProcessMemory(Pointer hProcess, long inBaseAddress, Pointer outputBuffer,
        int nSize, IntByReference outNumberOfBytesRead);

    Pointer OpenProcess(int desired, boolean inherit, int pid);
}
public interface User32 extends W32APIOptions {
    Pointer FindWindowA(String winClass, String title);

    int GetWindowThreadProcessId(Pointer hWnd, IntByReference lpdwProcessId);
}

0 个答案:

没有答案