本文为大家介绍loadlibrary 找不到指定的程序(loadlibrary failed with error 126:找不到指定模块),下面和小编一起看看详细内容吧。
部分用户在编写和调用dll文件时出错,收到提示loadlibrary failed witherror126: the specified module could not be found。这是怎么回事?为什么会这样?让我们来看看详细的分析和解决方案。
一、loadlibrary失败的原因
通常,loadlibrary 失败的原因是代码编写不规范。写一个dll文件一般不难,关键是写dll的时候代码不规范,所以调用的时候可能会出现这种问题,loadlibrary失败。难怪,为了确保您使用正确的调用约定,告诉编译器使用stdcall 约定和/或使用windows.h(和相关文件)中定义的常量,例如winapi 等。通常dll 的代码是如下:
01 word winapi vbshiftright(word nvalue,word nbits)
03 返回(nvalue nbits);
复制代码
word winapi vbshiftright(word nvalue, word nbits) { return (nvalue nbits);}
下一步与您在microsoft 文档中阅读的内容相反。您需要创建一个def 文件。这是唯一可以防止输出不带乱码的函数名(如_vbshiftright@1)的方法。 def文件的格式如下:
01出口
02vb右移
复制代码
导出vbshiftright
下一步是在vb 中调用此函数,使用以下声明:
01 声明函数vbshiftright lib mydll.dll (byval nvalue as integer,
02 byval nbits 作为整数)
03 作为整数
04 子测试()
05 将我调暗为整数
06 我=vbshiftright(4, 2)
07调试。断言i=1
08 结束子
复制代码
声明函数vbshiftright lib mydll.dll (byval nvalue as integer, byvalnbits as integer) as integer sub test() dim i as integer i=vbshiftright(4,2) debug.assert i=1 end sub
如果您还希望更简单的方法从vb 调用,您可以创建一个类型库。为此,您需要创建和编译odl(对象描述语言)文件。该文件应包含以下内容:
01 模块mymodule {
03 helpstring(将整数的位右移。),
04条目(vbshiftright)
06 short _stdcall vbshiftright([in] short nvalue, [in] short nbits);
07};
复制代码
module mymodule { [ helpstring( 将整数的位向右移动。 ),entry( vbshiftright ) ] short _stdcall vbshiftright([in] short nvalue, [in] short nbits); };
当vb 加载dll 的类型库时,函数名和参数将出现在vb 的对象浏览器中。此外,如果用户没有输入正确的参数类型,vb 可能会产生loadlibrary 失败错误。
另外,你最好使用正确的方法调用dll,下面是我通常调用dll的函数:
01 typedef void __declspec(dllimport) startqueryform(tdispatchconnection,tapplication);
02 startqueryform*查询;
03 字符缓冲区[256];
04 如果(!getsystemdirectory(buf,256)){
05application- messagebox(读取系统目录错误,error, mb_ok+mb_iconerror);
06 返回;
08 ansistring scmd=ansistring(buf)+ \\queryenh.dll 文件
复制代码
typedefvoid __declspec(dllimport)startqueryform(tdispatchconnection,tapplication); startqueryform* 查询;char buf[256]; if (!getsystemdirectory(buf,256)) {application- messagebox(读取系统目录错误,error, mb_ok+mb_iconerror);返回; }ansistring scmd=ansistring(buf)+ \\queryenh.dll
01 hinstance 包=loadlibrary(scmd.c_str());
02 如果(包)
04 试试{
05 查询=(startqueryform *)getprocaddress((hinstance)package, _startqueryform
06 如果(查询){
07 tdispatchconnection* conn=(mainform-connectionway==1 ?
08(tdispatchconnection*)mainform-dcomconnect:
09(tdispatchconnection*)mainform-sockconnect);
10查询(连接,应用程序);
12 其他{
13 ansistring str=函数加载失败,失败原因:\r
14str+=syserrormessage(getlasterror());
15application- messagebox(str.c_str(), error ,mb_ok+mb_iconerror);
18 __终于{
19freelibrary(包);
22其他
24 ansistring str=加载库失败,失败原因:\r
25str+=syserrormessage(getlasterror());
26application-messagebox(str.c_str(),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=加载函数失败,失败原因:\r str+=syserrormessage(getlasterror());application-messagebox(str.c_str(), error,mb_ok+mb_iconerror); } } __finally {freelibrary(package); } } else { ansistring str=加载库失败,失败原因:\r str+=syserrormessage(getlasterror());application- messagebox(str.c_str(),mb_ok+mb_iconerror);
2、loadlibrary失败的解决方法
方法一:使用loadlibraryex
如果dll与调用者不在同一个目录下,可以用loadlibrary(l dll绝对路径)加载。但是,如果被调用的dll在内部又调用了另一个dll,此时调用仍然会失败。
解决方案是使用loadlibraryex:
loadlibraryex( dll 绝对路径, null, load_with_altered_search_path);
通过指定load_with_altered_search_path,系统dll搜索顺序从dll所在目录开始。
方法2:使用setcurrentdir
要跨目录调用dll,你应该这样做
1. 使用getcurrentdir 保存当前工作目录。
2、使用setcurrentdir将当前工作目录设置为你的dll所在的路径,需要使用绝对路径。
3. 为您的dll 使用loadlibrary。
4. 使用setcurrentdir 恢复到原来的工作路径。
好了,loadlibrary 找不到指定的程序(loadlibrary failed with error 126:找不到指定模块)的介绍到这里就结束了,想知道更多相关资料可以收藏我们的网站。
怎么彻底关闭win10自动更新详细教程(怎么彻底关闭win10自动更新详细教程图片)
红米1S内存不足怎么解决,为什么我的红米1s老是提醒外部储存器空间不够
惠普打印机安装驱动程序时找不到打印机(打惠普印机找不到驱动程序怎么弄)
固态硬盘不分区需要4k对齐吗,diskgenius固态硬盘分区4k对齐
监控电脑桌面的软件(电脑隐形监控软件)
loadlibrary 找不到指定的程序(loadlibrary failed with error 126-找不到指定模块)
mml码无效怎么回事(mml码无效肿么办)
三星exynos处理器的优势与劣势(三星cpuexynos怎么样)
动态硬盘和固态硬盘哪个好一点,固态硬盘变成动态硬盘
英特尔 13代,13代酷睿l2缓存到了32m睿频有望6g锐龙7000缓存80m睿频58g哪个强
固态256G几个分区,win10固态256g最佳分区
哈尔滨电信4g网络怎么样,在哈尔滨中国电信4G的信号和网速和其他两家比怎么样
固态硬盘装系统win10详细教程(固态装win10详细教程)
一万元左右男士手表哪个品牌好(一万元男士手表品牌选择)
word2003点击打印按钮,直接打印,没有(word文档点击打印的时候就不动了)
sony z2怎么换镜头,sony Xperia z2 换一个后置摄像头后盖需要多少钱
电脑突然没声音的原因及解决方案(电脑突然没声音的原因及解决方案怎么回事)
永劫无间启动游戏未响应(永劫无间启动游戏后没反应)
免费秒玩所有游戏的软件还不限时间(免费秒玩大型游戏软件有哪些)
电脑硬盘合并教程,怎样将磁盘合并在一起