2016-08-04 14 views
0
은 다음 코드는 발견하지

null를 돌려 GetProcAddress를이 다음과 같이 선언된다 LoadLibraryW자바 JNA : GetProcAddress를이

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; 
} 

:

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); 
} 

사람이 왜 알고 있나요? 내 실수는 무엇입니까? 감사합니다.

+0

GetProcAddress()가 null을 반환 할 때 마지막으로보고되는 오류는 무엇입니까? 그리고 왜'GetProcAddress()'가'FOREIGN_THREAD_START_ROUTINE'을 리턴한다고 선언하고 있습니까? 그것은 실제로'GetProcAddress()'가 실제로 반환하는 것이 아닙니다. 그것은 기본적으로 포인터 인'FARPROC'을 반환하고, 그래서 JNA에서'LPVOID'가됩니다. 실제로 함수를 호출해야 할 때 포인터를 타입 캐스트합니다. –

+4

네이티브'GetProcAddress' 호출에 대한 seciond 인자로 ANSI 문자열을 전달하고 있습니까? 다른 WIndows API 펑크 션과 달리'GetProcAddress'는 수출 된 함수의 이름이 ANSI 문자를 사용하여 작성되기 때문에 유니 코드 버전이 없습니다. –

답변

0

Martin Drab이 옳았습니다. W32APIOptions.UNICODE_OPTIONS를 사용하면 유니 코드 문자열이 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); 
}