我正在尝试从记事本实例的内存中读取一部分,但是在调用kernel32的ReadProcessMemory()
时总是会收到系统错误299。
这是我到目前为止的代码:
package memreadtest;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.W32APIOptions;
public class MemReader {
private final static Kernel32 kernel32 = Native.load("kernel32", Kernel32.class, W32APIOptions.DEFAULT_OPTIONS);
private final static User32 user32 = Native.load("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
private final static int PROCESS_VM_READ = 0x0010;
public static void main(String[] args) {
int bytesToRead = 1024;
Memory notepadDump = readProcessMemory("*Untitled - Notepad", bytesToRead);
Memory.disposeAll();
}
private static Memory readProcessMemory(String winTitle, int bytesToRead) {
Memory output = new Memory(bytesToRead);
IntByReference pid = new IntByReference(0);
user32.GetWindowThreadProcessId(user32.FindWindow(null, winTitle), pid);
HANDLE handle = kernel32.OpenProcess(PROCESS_VM_READ, true, pid.getValue());
if (!kernel32.ReadProcessMemory(handle, handle.getPointer(), output, bytesToRead, null)) {
System.err.println("Failed to read memory of process " + pid.getValue() + ". System Error Code: " + kernel32.GetLastError());
return null;
}
return output;
}
}
我在做什么错?
您正在读取一个没有理由期望在外部流程中有效的地址。进程句柄不应是该进程中的有效地址。
我不确定您要从此过程中读取什么,但是不管它是什么,您都需要以某种方式找到合适的地址。
您在注释中阐明了要从模块基地址读取的内容。例如,请参见:Get base address of process