Java JNA:GetProcAddress returns 空
Java JNA: GetProcAddress returns null
以下代码无法找到 LoadLibraryW
private static FOREIGN_THREAD_START_ROUTINE getLoadLibraryWAddress() throws Win32Exception {
HMODULE module = Kernel32.INSTANCE.GetModuleHandle("KERNEL32");
if(module == null) {
Win32Exception.throwWithLastError("Failed to find KERNEL32 module");
}
FOREIGN_THREAD_START_ROUTINE address = Kernel32MissingFunctions.INSTANCE.GetProcAddress(module, "LoadLibraryW");
if(address == null) {
Win32Exception.throwWithLastError("Failed to find LoadLibraryW in KERNEL32 module");
}
return address;
}
其中 GetProcAddress 声明如下:
public interface Kernel32MissingFunctions extends StdCallLibrary {
Kernel32MissingFunctions INSTANCE = (Kernel32MissingFunctions) Native.loadLibrary("kernel32",
Kernel32MissingFunctions.class, W32APIOptions.UNICODE_OPTIONS);
public static final int MEM_RELEASE = 0x8000;
public LPVOID VirtualAllocEx(HANDLE hProcess, LPVOID lpAddress, long dwSize, int flAllocationType, int flProtect);
public int VirtualFreeEx(HANDLE hProcess, LPVOID lpAddress, long dwSize, int dwFreeType);
public FOREIGN_THREAD_START_ROUTINE GetProcAddress(HMODULE hModule, String lpProcName);
}
有谁知道为什么?我的错误是什么?
谢谢!
Martin Drab 是对的。使用 W32APIOptions.UNICODE_OPTIONS 将 Unicode 字符串传递给 Ansi 函数。如下更改接口修复了问题:
public interface Kernel32MissingFunctions extends StdCallLibrary {
Kernel32MissingFunctions INSTANCE = (Kernel32MissingFunctions) Native.loadLibrary("kernel32",
Kernel32MissingFunctions.class, W32APIOptions.ASCII_OPTIONS);
public static final int MEM_RELEASE = 0x8000;
public LPVOID VirtualAllocEx(HANDLE hProcess, LPVOID lpAddress, long dwSize, int flAllocationType, int flProtect);
public int VirtualFreeEx(HANDLE hProcess, LPVOID lpAddress, long dwSize, int dwFreeType);
public LPVOID GetProcAddress(HMODULE hModule, String lpProcName);
}
以下代码无法找到 LoadLibraryW
private static FOREIGN_THREAD_START_ROUTINE getLoadLibraryWAddress() throws Win32Exception {
HMODULE module = Kernel32.INSTANCE.GetModuleHandle("KERNEL32");
if(module == null) {
Win32Exception.throwWithLastError("Failed to find KERNEL32 module");
}
FOREIGN_THREAD_START_ROUTINE address = Kernel32MissingFunctions.INSTANCE.GetProcAddress(module, "LoadLibraryW");
if(address == null) {
Win32Exception.throwWithLastError("Failed to find LoadLibraryW in KERNEL32 module");
}
return address;
}
其中 GetProcAddress 声明如下:
public interface Kernel32MissingFunctions extends StdCallLibrary {
Kernel32MissingFunctions INSTANCE = (Kernel32MissingFunctions) Native.loadLibrary("kernel32",
Kernel32MissingFunctions.class, W32APIOptions.UNICODE_OPTIONS);
public static final int MEM_RELEASE = 0x8000;
public LPVOID VirtualAllocEx(HANDLE hProcess, LPVOID lpAddress, long dwSize, int flAllocationType, int flProtect);
public int VirtualFreeEx(HANDLE hProcess, LPVOID lpAddress, long dwSize, int dwFreeType);
public FOREIGN_THREAD_START_ROUTINE GetProcAddress(HMODULE hModule, String lpProcName);
}
有谁知道为什么?我的错误是什么? 谢谢!
Martin Drab 是对的。使用 W32APIOptions.UNICODE_OPTIONS 将 Unicode 字符串传递给 Ansi 函数。如下更改接口修复了问题:
public interface Kernel32MissingFunctions extends StdCallLibrary {
Kernel32MissingFunctions INSTANCE = (Kernel32MissingFunctions) Native.loadLibrary("kernel32",
Kernel32MissingFunctions.class, W32APIOptions.ASCII_OPTIONS);
public static final int MEM_RELEASE = 0x8000;
public LPVOID VirtualAllocEx(HANDLE hProcess, LPVOID lpAddress, long dwSize, int flAllocationType, int flProtect);
public int VirtualFreeEx(HANDLE hProcess, LPVOID lpAddress, long dwSize, int dwFreeType);
public LPVOID GetProcAddress(HMODULE hModule, String lpProcName);
}