LoadLibrary失败

LoadLibrary失败LoadLibrary失败 今天同事遇到一个问题,经高手指点,完美解决。不过解决方法总是感觉有点不妥,不知道有没有其它方法。 正常情况,在一个exe中LoadLibrary(DLL1)可以获得正常的结果; 但是,当我们需要load的DLL1如果调用了其它的DLL2,那么我们就会得到一个结果:Theprogramcantstartbecause****.dl

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

LoadLibrary失败

 

今天同事遇到一个问题,经高手指点,完美解决。不过解决方法总是感觉有点不妥,不知道有没有其它方法。

 

正常情况,在一个exe中LoadLibrary(DLL1)可以获得正常的结果;

 

但是,当我们需要load的DLL1如果调用了其它的DLL2, 那么我们就会得到一个结果:

The program can’t start because ****.dll is missing from you computer. Try reinstalling the program to fix this problem.

 

 

用@err,hr查看:The specified module could not be found.

 

注意:我这里讨论的是指Dll1与Dll2在同一个目录,Dll1与Dll2均是自己定义的DLL,而exe与这两个dll不在同一个目录。

 

讨论正常的情况:     如果Dll1与Dll2没有关系,即Dll1是独立的,它不依赖其它自定义的Dll, 则我们LoadLibrary(Dll1的绝对路径)肯定能成功。

讨论不正常的情况:如果Dll1与Dll2有关系,即Dll1不是独立的,它依赖自定义的Dll2, 则我们LoadLibrary(Dll1的绝对路径)时候就会出现上面的情况。

 

为什么呢?

按理说,Dll1的绝对路径都写上了,明明是对的,可为什么得到的返回值是0x00000000,而且说module找不到??虽然Dll1依赖于Dll2,但是Dll1与Dll2是在同一个目录下啊,怎么找不到呢?

其实,这里分两个步骤,LoadLibrary首先会去加载Dll1, 然后加载它依赖的Dll2. 对于Dll1,因为有绝对路径,所以能找到,但是,对于Dll2来说,我们却找不到,尽管Dll1与Dll2在同一个目录。LoadLibrary只管你指明要加载的DLL,它才不会去主动寻找你依赖的DLL。因此这就造成了module找不到,这里说的找不到module一般都是指找不到依赖的DLL。

 

至于LoadLibrary的查找路径,可以参见MSDN上的文章:Dynamic-Link Library Search Order。

默认情况如下:

The directory from which the application loaded. (应用程序所在的目录)
The system directory. Use the GetSystemDirectory function to get the path of this directory. (system32目录)
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. (System目录)
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. (Windows目录)
The current directory. (不清楚这个目录和应用程序所在的目录有什么区别)
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. (PATH路径)
 

解决办法:其实我们只需要将Dll2的路径添加到LoadLibrary的查找目录就可以了。

1. 在Dll1的工程属性->Linker->Advanced下修改Delay Loaded DLL,将之改为Support Unload (/DELAY:UNLOAD);

2. 在Dll1的工程属性->Linker->Input下修改Delay Loaded DLLs,在后面添加Dll2;

3. 在Dll1的代码中添加一些内容。我这里选择DllMain。

     case DLL_PROCESS_ATTACH:

SetDllDirectory(Dll2的路径);

break;

     case DLL_THREAD_ATTACH:

SetDllDirectory(Dll2的路径);

break;

 

 

那么SetDllDirectory 有何功效呢?参见MSDN。

The SetDllDirectory function affects all subsequent calls to the LoadLibrary and LoadLibraryEx functions. It also effectively disables safe DLL search mode while the specified directory is in the search path.

After calling SetDllDirectory, the DLL search path is:

The directory from which the application loaded.
The directory specified by the lpPathName parameter. (添加了DLL Search的路径)
The system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable.
To revert to the default search path used by LoadLibrary and LoadLibraryEx, call SetDllDirectory with NULL. This also restores safe DLL search mode based on the SafeDllSearchMode registry value.

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/woyaowenzi/archive/2009/07/08/4332187.aspx

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158338.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • elasticsearch集群搭建的过程_elastic公司

    elasticsearch集群搭建的过程_elastic公司elasticsearch集群搭建(Windows)1、下载es:https://www.elastic.co/cn/downloads/past-releases#elasticsearch进入页面,选择版本,选择windows版本2、解压下载的es,拷贝3份(搭建几个节点就复制几分)3、修改配置文件注意配置文件key:value冒号之后必须有个空格,否则启动报…

    2022年10月13日
  • pycharm如何调试代码_pycharm怎么分段运行代码

    pycharm如何调试代码_pycharm怎么分段运行代码最全Pycharm教程(1)——定制外观  最全Pycharm教程(2)——代码风格  1、准备工作  (1)Python版本为2.7或者更高版本  (2)已经创建了一个python工程并且添加了内容,具体参考: GettingStartedtutorial  2、第一步——运行代码  打开之前编写的Solver.py文件,在编辑框中右键,选

    2022年8月27日
  • PXE及PXE启动

    PXE及PXE启动PXE(Pre-bootExecutionEnvironment)是由Intel设计的协议,它可以使计算机通过网络启动。协议分为client和server两端,PXEclient在网卡的ROM中,当计算机引导时,BIOS把PXEclient调入内存执行,并显示出命令菜单,经用户选择后,PXEclient将放置在远端的操作系统通过网络下载到本地运行。  PXE协议的成功运行需要解决以下

    2022年6月15日
  • JDBC连接数据库6个步骤

    JDBC连接数据库6个步骤JDBC连接数据库,创建一个以JDBC连接数据库的程序,包含7个步骤:   首先准备JDBC所需的四个参数(user,password,url,driverClass)(1)user用户名(2)password密码(3)URL定义了连接数据库时的协议、子协议、数据源标识,它们之间用冒号隔开。  书写形式:  协议:子协议:数据源标识       协议:在JDBC中总是以jdb…

    2022年7月12日
  • Unreal Engine 4 C++ 能够创建角色Zoom摄像头(资源)

    Unreal Engine 4 C++ 能够创建角色Zoom摄像头(资源)

    2022年1月14日
  • Typescript教程_安装typescript

    Typescript教程_安装typescript前言由于最近在使用vue3写项目,使用vue3的前提就是要学习TypeScript,TypeScript算是JavaScript的升级版,TypeScript包含JavaScript和自己的一些特性

    2022年7月29日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号