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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 十折交叉验证 matlab,Matlab 十折交叉验证

    十折交叉验证 matlab,Matlab 十折交叉验证十折交叉验证(1)英文名叫做10-foldcross-validation,用来测试算法准确性,是常用的测试方法。(2)将数据集分成十份,轮流将其中9份作为训练数据,1份作为测试数据,进行试验。每次试验都会得出相应的正确率(或差错率)。(3)10次的结果的正确率(或差错率)的平均值作为对算法精度的估计,一般还需要进行多次10折交叉验证(例如10次10折交叉验证),再求其均值,作为对算法准确性的估…

    2025年8月11日
    2
  • CMS-项目的技术架构

    CMS-项目的技术架构2项目的技术架构2.1技术架构学成在线采用当前流行的前后端分离架构开发,由用户层、UI层、微服务层、数据层等部分组成,为PC、App、H5等客户端用户提供服务。下图是系统的技术架构图:业务流程举例:用户可以通过pc、手机等客户端访问系统进行在线学习。系统应用CDN技术,对一些图片、CSS、视频等资源从CDN调度访问。所有的请求全部经过负载均衡器。对于PC、H5等客户端请求,…

    2022年6月4日
    47
  • sourceinsight4.0注册码_sourceinsight激活成功教程版安装教程

    sourceinsight4.0注册码_sourceinsight激活成功教程版安装教程SourceInsight3.x注册码:SI3US-205035-36448验证可用

    2022年10月3日
    4
  • ROC曲线与AUC

    ROC曲线与AUC对于0,1两类分类问题,一些分类器得到的结果往往不是0,1这样的标签,如神经网络,得到诸如0.5,0,8这样的分类结果.这时,我们人为取一个阈值,比如0.4,那么小于0.4的为0类,大于等于0.4的为1类,可以得到一个分类结果。同样,这个阈值我们可以取0.1,0.2等等。取不同的阈值,得到的最后的分类情况也就不同。阈值不同,可以得到不同的结果,但是由分类器决定的统计图始终是不变的。这时候就需要一个独立与阈值,只与分类器有关的评价指标,来衡量特定分类器的好坏。还有在类不平衡的情况下,如正样本90个,负样本

    2022年5月13日
    45
  • java gridbagconstraints_GridBagConstraints

    java gridbagconstraints_GridBagConstraintsGridBagConstraintsjavaGui|2014-05-1412:54:00|阅读1737次|评论(1)importjavax.swing.*;importjava.awt.*;publicclassdemo_7{publicstaticvoidmain(String[]args){demo_7_1test=newdemo_7_1();}}clas…

    2025年10月17日
    3
  • Ubuntu20.04安装中文输入法

    Ubuntu20.04安装中文输入法新装了ubuntu20.04的系统,由于装系统时,选择安装了英文的版本,因此系统没有中文输入法,此时我想加入中文输入法。在设置-语言中怎么改都没有用。最终用下面方法解决了安装中文语言包**由于我装系统时,选择了安装English版本,所以先安装了一下中文语言包。sudoapt-getupdatesudoapt-getinstalllanguage-pack-zh-hans安装输入法sudoaptinstallibus-libpinyinsudoapti

    2022年9月25日
    3

发表回复

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

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