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


相关推荐

  • python suds_python suds 一坑

    python suds_python suds 一坑当被调用服务的返回xml内容值不是按照wsdl文件描述定义的,就莫名奇妙返回suds.WebFault没有更多详细信息!于是将源码解压,并插入到sys.path[0],通过设置断点的方式找出非标准的返回报文到底在说啥。从而调整对应参数。suds-0.4/suds/bindings/binding.py(246~268)defget_fault(self,reply):”””Extract…

    2022年10月23日
    0
  • VS2017配置opencv教程(超详细!!!)[通俗易懂]

    VS2017配置opencv教程(超详细!!!)[通俗易懂]前言:刚开始是不打算写这篇博客的,但是我最近为了完成对老师布置的区域生长算法,强行要配置一波opencv,因为换了电脑,所以选择了有黑黑主题酷酷的VS2017,但无奈网上的博客关于vs2017配置Opencv不够全(vs2010配置opencv的倒是贼多!),我当时是看了将近10篇文章才配置成功,所以在此我来从一个新入坑的角度来说一下怎么完整的配置一个OPencv!下面就分步来进行说明吧!…

    2022年6月13日
    23
  • python-tkinter(7) 实现各种个样的撩妹鼠标拖尾

    python-tkinter(7) 实现各种个样的撩妹鼠标拖尾

    2022年2月21日
    45
  • es6数组方法find()、findIndex()与filter()的总结

    es6数组方法find()、findIndex()与filter()的总结find()该方法主要应用于查找第一个符合条件的数组元素。它的参数是一个回调函数。在回调函数中可以写你要查找元素的条件,当条件成立为true时,返回该元素。如果没有符合条件的元素,返回值为undefined。以下代码在myArr数组中查找元素值大于4的元素,找到后立即返回。返回的结果为查找到的元素:constmyArr=[1,2,3,4,5,6];varv=myArr.find(value=>value>4);console.log(v);//5没有符合元素,返回undefi

    2022年5月29日
    78
  • propertydescriptor是用来干什么的_constructor java

    propertydescriptor是用来干什么的_constructor java1、PropertyDescriptor简述PropertyDescriptor对象是位于java.beans包下的工具类,顾名思义为属性描述器,通常我们用于通过反射获取对象方法的时候,下面来看一下常用的用法吧!2、PropertyDescriptor用法(1)、给你一个java对象,你如何生成PropertyDescriptor对象呢?通常,我们会用到…

    2022年10月1日
    0
  • Spring3 MVC请求参数获取的几种方法

     一、      通过@PathVariabl获取路径中的参数  @RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET) public String printMessage1(@PathVariable String id,@PathVariable String name, ModelMa…

    2022年2月24日
    61

发表回复

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

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