LoadLibrary失敗的原因(轉)


背影:

    今天終於把公司的SDK 動態鏈接庫轉為Java 可調用的JNI 格式。DLL的編譯環境是VS2010,使用Debug 輸出時調用正常,而用Release 輸出卻調用失敗。這可把哥搞慘了,開始以為是編譯設置問題,找了N多方法均不得解。最后只得用哥慣用的調試手段,在DLL 里加打印消息一步步確認出錯位置,然后得知是DLL 里調用公司的DLL 不成功。隨后就找到了下面的方法,一試得解。非常謝該網友。

 

源:LoadLibrary失敗的原因

今天使用LoadLibrary時,失敗,於是翻了一下MSDN:

 

LoadLibrary The LoadLibrary function maps the specified executable module into the address space of the calling process. 
For additional load options, use the LoadLibraryEx function. 
HMODULE LoadLibrary(   LPCTSTR lpFileName   // file name of module ); Parameters lpFileName  [in] Pointer to a null-terminated string that names the executable module (either a .dll or .exe file). The name specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.def) file.  If the string specifies a path but the file does not exist in the specified directory, the function fails. When specifying a path, be sure to use backslashes (\), not forward slashes (/). 
If the string does not specify a path, the function uses a standard search strategy to find the file. See the Remarks for more information. 
Return Values If the function succeeds, the return value is a handle to the module.
If the function fails, the return value is NULL. To get extended error information, call GetLastError. 
Windows 95: If you are using LoadLibrary to load a module that contains a resource whose numeric identifier is greater than 0x7FFF, LoadLibrary fails. If you are attempting to load a 16-bit DLL directly from 32-bit code, LoadLibrary fails. If you are attempting to load a DLL whose subsystem version is greater than 4.0, LoadLibrary fails. If your DllMain function tries to call the Unicode version of a Win32 function, LoadLibrary fails. 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

注意上面紅色的文字,如果我們在dll的入口函數DllMain()中調用Unicode版本的系統API,則

LoadLibrary加載我們的dll文件時會失敗,這就是我這次加載dll失敗的原因,將API改為A版本的就

正常了,如GetModuleFileNameA

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM