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

默认情况如下:

  1. The directory from which the application loaded. (应用程序所在的目录)
  2. The system directory. Use the GetSystemDirectory function to get the path of this directory. (system32目录)
  3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. (System目录)
  4. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. (Windows目录)
  5. The current directory. (不清楚这个目录和应用程序所在的目录有什么区别)
  6. 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:

  1. The directory from which the application loaded.
  2. The directory specified by the lpPathName parameter. (添加了DLL Search的路径)
  3. The system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.
  4. 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.
  5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
  6. 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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值