系統(tǒng)之家 - 系統(tǒng)光盤下載網(wǎng)站!

當(dāng)前位置:系統(tǒng)之家 > 系統(tǒng)教程 > 裝載dll文件提示“LoadLibrary失敗”怎

裝載dll文件提示“LoadLibrary失敗”怎么解決?

時(shí)間:2017-11-04 09:01:00 作者:chunhua 來源:系統(tǒng)之家 1. 掃描二維碼隨時(shí)看資訊 2. 請使用手機(jī)瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20171104/110308.html 手機(jī)查看 評(píng)論

  裝載dll文件提示“LoadLibrary失敗”怎么解決?一用戶在開發(fā)程序時(shí)寫了一個(gè)dll文件,但是在調(diào)用dll文件時(shí)出錯(cuò)了,提示“LoadLibrary failed with error126:找不到指定的模塊。”,這是怎么回事呢?下面小編給大家分析出現(xiàn)LoadLibrary失敗的原因及解決辦法。

裝載dll文件提示“LoadLibrary失敗”怎么解決?

  一、出現(xiàn)LoadLibrary失敗的原因

  通常LoadLibrary失敗的原因大多是代碼書寫不規(guī)范,編寫dll文件一般不是很難,但關(guān)鍵是在寫dll的時(shí)候代碼不規(guī)范,這樣在調(diào)用時(shí)就有可可能出現(xiàn)這樣那樣的問題,出現(xiàn)LoadLibrary失敗也就不足為怪了,為了保證你使用正確的調(diào)用規(guī)范,要通知編譯器使用stdcall規(guī)范和/或使用在windows.h(及相關(guān)文件)中定義的常量,如WINAPI等。通常DLL的代碼如下:

  1. 01WORD WINAPI vbShiftRight(WORD nValue, WORD nBits)
  2. 02{
  3. 03return (nValue >> nBits);
  4. 04}
復(fù)制代碼
WORD WINAPI vbShiftRight(WORD nValue, WORD nBits) { return (nValue >> nBits); }

  下一步是與你在微軟文檔中讀到的內(nèi)容相反。你需要?jiǎng)?chuàng)建一個(gè)DEF文件。這是你防止輸出函數(shù)名不出現(xiàn)亂字符的唯一方式(如_vbShiftRight@1)。DEF文件的形式如下:

  1. 01EXPORTS
  2. 02vbShiftRight
復(fù)制代碼
EXPORTS vbShiftRight

  下一步是在VB中調(diào)用這個(gè)函數(shù),使用以下聲明:

  1. 01Declare Function vbShiftRight Lib "MYDLL.DLL" (ByVal nValue As Integer,
  2. 02ByVal nBits As Integer)
  3. 03As Integer
  4. 04Sub Test()
  5. 05Dim i As Integer
  6. 06i = vbShiftRight(4, 2)
  7. 07Debug.Assert i = 1
  8. 08End Sub
復(fù)制代碼
Declare Function vbShiftRight Lib "MYDLL.DLL" (ByVal nValue As Integer, ByVal nBits As Integer) As Integer Sub Test() Dim i As Integer i = vbShiftRight(4, 2) Debug.Assert i = 1 End Sub

  如果你還想要更容易的方法從VB中調(diào)用,可以創(chuàng)建一個(gè)類型庫。為此你需要?jiǎng)?chuàng)建和編譯ODL(對(duì)象描述語言)文件。這個(gè)文件應(yīng)該包含如下內(nèi)容:

  1. 01module MyModule {
  2. 02[
  3. 03helpstring("Shifts the bits of an integer to the right."),
  4. 04entry("vbShiftRight")
  5. 05]
  6. 06short _stdcall vbShiftRight([in] short nValue, [in] short nBits);
  7. 07};
復(fù)制代碼
module MyModule { [ helpstring("Shifts the bits of an integer to the right."), entry("vbShiftRight") ] short _stdcall vbShiftRight([in] short nValue, [in] short nBits); };

  當(dāng)VB加載DLL的類型庫時(shí),函數(shù)名和參數(shù)將出現(xiàn)在VB的對(duì)象瀏覽器中。此外,如果用戶不輸入正確的參數(shù)類型,VB將有可能產(chǎn)生LoadLibrary失敗錯(cuò)誤。

  還有就是你最好用正確的方法調(diào)用dll,以下是我正常調(diào)用dll的函數(shù):

  1. 01typedef void __declspec(dllimport) StartQueryForm(TDispatchConnection*,TApplication*);
  2. 02StartQueryForm* query;
  3. 03char buf[256];
  4. 04if (!GetSystemDirectory(buf,256)) {
  5. 05Application->MessageBox("讀取系統(tǒng)目錄錯(cuò)誤","錯(cuò)誤",MB_OK+MB_ICONERROR);
  6. 06return ;
  7. 07}
  8. 08AnsiString sCmd=AnsiString(buf)+"\\QueryEnh.dll";
復(fù)制代碼
typedef void __declspec(dllimport) StartQueryForm(TDispatchConnection*,TApplication*); StartQueryForm* query; char buf[256]; if (!GetSystemDirectory(buf,256)) { Application->MessageBox("讀取系統(tǒng)目錄錯(cuò)誤","錯(cuò)誤",MB_OK+MB_ICONERROR); return ; } AnsiString sCmd=AnsiString(buf)+"\\QueryEnh.dll";
  1. 01HINSTANCE Package = LoadLibrary(sCmd.c_str());
  2. 02if (Package)
  3. 03{
  4. 04try {
  5. 05query = (StartQueryForm *)GetProcAddress((HINSTANCE)Package, "_StartQueryForm");
  6. 06if (query) {
  7. 07TDispatchConnection* conn=(MainForm->ConnectionWay==1 ?
  8. 08(TDispatchConnection*)MainForm->dcomConnect:
  9. 09(TDispatchConnection*)MainForm->sockConnect);
  10. 10query(conn,Application);
  11. 11}
  12. 12else {
  13. 13AnsiString str="加載函數(shù)失敗,失敗原因:\n\r";
  14. 14str+=SysErrorMessage(GetLastError());
  15. 15Application->MessageBox(str.c_str(),"錯(cuò)誤",MB_OK+MB_ICONERROR);
  16. 16}
  17. 17}
  18. 18__finally {
  19. 19FreeLibrary(Package);
  20. 20}
  21. 21}
  22. 22else
  23. 23{
  24. 24AnsiString str="加載庫失敗,失敗原因:\n\r";
  25. 25str+=SysErrorMessage(GetLastError());
  26. 26Application->MessageBox(str.c_str(),"´íÎó",MB_OK+MB_ICONERROR);
復(fù)制代碼
HINSTANCE Package = LoadLibrary(sCmd.c_str()); if (Package) { try { query = (StartQueryForm *)GetProcAddress((HINSTANCE)Package, "_StartQueryForm"); if (query) { TDispatchConnection* conn=(MainForm->ConnectionWay==1 ? (TDispatchConnection*)MainForm->dcomConnect: (TDispatchConnection*)MainForm->sockConnect); query(conn,Application); } else { AnsiString str="加載函數(shù)失敗,失敗原因:\n\r"; str+=SysErrorMessage(GetLastError()); Application->MessageBox(str.c_str(),"錯(cuò)誤",MB_OK+MB_ICONERROR); } } __finally { FreeLibrary(Package); } } else { AnsiString str="加載庫失敗,失敗原因:\n\r"; str+=SysErrorMessage(GetLastError()); Application->MessageBox(str.c_str(),"´íÎó",MB_OK+MB_ICONERROR);

  二、出現(xiàn)LoadLibrary失敗解決辦法

  方式一:采用LoadLibraryEx

  若DLL不在調(diào)用方的同一目錄下,可以用LoadLibrary(L“DLL絕對(duì)路徑”)加載。但若調(diào)用的DLL內(nèi)部又調(diào)用另外一個(gè)DLL,此時(shí)調(diào)用仍會(huì)失敗。

  解決辦法是用LoadLibraryEx:

  LoadLibraryEx("DLL絕對(duì)路徑", NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 

  通過指定LOAD_WITH_ALTERED_SEARCH_PATH,讓系統(tǒng)DLL搜索順序從DLL所在目錄開始。

  方式二:采用SetCurrentDir

  跨目錄調(diào)用dll,你應(yīng)該這樣

  1、用GetCurrentDir保存當(dāng)前的工作目錄

  2、用SetCurrentDir將當(dāng)前的工作目錄,設(shè)置為你的DLL所在的路徑,需要使用絕對(duì)路徑

  3、用LoadLibrary你的DLL

  4、使用SetCurrentDir恢復(fù)到原來的工作路徑

  以上便是裝載dll文件提示“LoadLibrary失敗”的原因及解決辦法,有遇到此錯(cuò)誤提示的伙伴,可以參考上文解決。

標(biāo)簽 dll文件

發(fā)表評(píng)論

0

沒有更多評(píng)論了

評(píng)論就這些咯,讓大家也知道你的獨(dú)特見解

立即評(píng)論

以上留言僅代表用戶個(gè)人觀點(diǎn),不代表系統(tǒng)之家立場

其他版本軟件

人氣教程排行

相關(guān)系統(tǒng)推薦

官方交流群 軟件收錄