大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/158338.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...