Java获取程序的基地址

时间:2019-04-02 15:19:56

标签: java memory jna

因此,我想获取程序的基地址。

我试图使用进程ID和指针(jna)来获取它,但是它总是返回0。

这是它的样子

  public static void main(String[] args) {
    enableDebugPrivilege(); 

    int pID = getProcessId(gameWindowName);
    System.out.println("Pid = " + pID); 

    int base = findBaseAddress(pID); 

    System.out.println("Base Address: " + Integer.toHexString((int) base));
    HANDLE game = openProcess(WinNT.PROCESS_QUERY_INFORMATION | WinNT.PROCESS_VM_READ, pID);

}

private static int findBaseAddress(int pID) {
    try {
        HANDLE game = openProcess(WinNT.PROCESS_QUERY_INFORMATION | WinNT.PROCESS_VM_READ, pID);
        List<Module> modules = PsapiTools.getInstance().EnumProcessModulesEx(game, 0x01);
        for (Module module : modules) {

            if (module.getBaseName().equals(gameExe)) {
                if (module.getLpBaseOfDll() != null) {
                    int baseAddress = ((Long) Pointer.nativeValue(module.getLpBaseOfDll().getPointer())).intValue();
                    return baseAddress;
                }
            }
        }
    } catch (Exception e) {
        System.out.println("Error finding base address");
        return -1;
    }
    return 0;
}

0 个答案:

没有答案