装载dll文件提示“LoadLibrary失败”怎么解决?

装载dll文件提示“LoadLibrary失败”怎么解决?一用户在开发程序时写了一个dll文件,但是在调用dll文件时出错了,提示“LoadLibrary failed with error126:找不到指定的模块 。”,这是怎么回事呢?下面小编给大家分析出现LoadLibrary失败的原因及解决办法 。

装载dll文件提示“LoadLibrary失败”怎么解决?

文章插图
一、出现LoadLibrary失败的原因
通常LoadLibrary失败的原因大多是代码书写不规范,编写dll文件一般不是很难,但关键是在写dll的时候代码不规范,这样在调用时就有可可能出现这样那样的问题,出现LoadLibrary失败也就不足为怪了,为了保证你使用正确的调用规范,要通知编译器使用stdcall规范和/或使用在windows.h(及相关文件)中定义的常量,如WINAPI等 。通常DLL的代码如下:
01WORD WINAPI vbShiftRight(WORD nValue, WORD nBits)02{03return (nValue >> nBits);04}复制代码WORD WINAPI vbShiftRight(WORD nValue, WORD nBits){return (nValue >> nBits);}下一步是与你在微软文档中读到的内容相反 。你需要创建一个DEF文件 。这是你防止输出函数名不出现乱字符的唯一方式(如_vbShiftRight@1) 。DEF文件的形式如下:
01EXPORTS02vbShiftRight复制代码EXPORTSvbShiftRight下一步是在VB中调用这个函数,使用以下声明:
01Declare Function vbShiftRight Lib "MYDLL.DLL" (ByVal nValue As Integer,02ByVal nBits As Integer)03As Integer04Sub Test()05Dim i As Integer06i = vbShiftRight(4, 2)07Debug.Assert i = 108End Sub复制代码Declare Function vbShiftRight Lib "MYDLL.DLL" (ByVal nValue As Integer,ByVal nBits As Integer)As IntegerSub Test()Dim i As Integeri = vbShiftRight(4, 2)Debug.Assert i = 1End Sub如果你还想要更容易的方法从VB中调用,可以创建一个类型库 。为此你需要创建和编译ODL(对象描述语言)文件 。这个文件应该包含如下内容:
01module MyModule {02[03helpstring("Shifts the bits of an integer to the right."),04entry("vbShiftRight")05]06short _stdcall vbShiftRight([in] short nValue, [in] short nBits);07};复制代码module MyModule {[helpstring("Shifts the bits of an integer to the right."),entry("vbShiftRight")]short _stdcall vbShiftRight([in] short nValue, [in] short nBits);};当VB加载DLL的类型库时,函数名和参数将出现在VB的对象浏览器中 。此外,如果用户不输入正确的参数类型,VB将有可能产生LoadLibrary失败错误 。
还有就是你最好用正确的方法调用dll,以下是我正常调用dll的函数:
01typedefvoid __declspec(dllimport) StartQueryForm(TDispatchConnection*,TApplication*);02StartQueryForm* query;03char buf[256];04if (!GetSystemDirectory(buf,256)) {05Application->MessageBox("读取系统目录错误","错误",MB_OK MB_ICONERROR);06return ;07}08AnsiString sCmd=AnsiString(buf) "QueryEnh.dll";复制代码typedefvoid __declspec(dllimport) StartQueryForm(TDispatchConnection*,TApplication*);StartQueryForm* query;char buf[256];if (!GetSystemDirectory(buf,256)) {Application->MessageBox("读取系统目录错误","错误",MB_OK MB_ICONERROR);return ;}AnsiString sCmd=AnsiString(buf) "QueryEnh.dll";01HINSTANCE Package = LoadLibrary(sCmd.c_str());02if (Package)03{04try {05query = (StartQueryForm *)GetProcAddress((HINSTANCE)Package, "_StartQueryForm");06if (query) {07TDispatchConnection* conn=(MainForm->ConnectionWay==1 ?08(TDispatchConnection*)MainForm->dcomConnect:09(TDispatchConnection*)MainForm->sockConnect);10query(conn,Application);11}12else {13AnsiString str="加载函数失败,失败原因:nr";14str =SysErrorMessage(GetLastError());15Application->MessageBox(str.c_str(),"错误",MB_OK MB_ICONERROR);16}17}18__finally {19FreeLibrary(Package);20}21}22else23{24AnsiString str="加载库失败,失败原因:nr";25str =SysErrorMessage(GetLastError());26Application->MessageBox(str.c_str(),"?iacute;?oacute;",MB_OK MB_ICONERROR);复制代码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="加载函数失败,失败原因:nr";str =SysErrorMessage(GetLastError());Application->MessageBox(str.c_str(),"错误",MB_OK MB_ICONERROR);}}__finally {FreeLibrary(Package);}}else{AnsiString str="加载库失败,失败原因:nr";str =SysErrorMessage(GetLastError());Application->MessageBox(str.c_str(),"?iacute;?oacute;",MB_OK MB_ICONERROR);二、出现LoadLibrary失败解决办法