dll 搜索路径

Safe DLL search mode is enabled by default. To disable this feature, create the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\SafeDllSearchMode registry value and set it to 0. Calling the SetDllDirectory function effectively disables SafeDllSearchMode while the specified directory is in the search path and changes the search order as described in this topic.

Windows XP: Safe DLL search mode is disabled by default. To enable this feature, create the SafeDllSearchMode registry value and set it to 1. Safe DLL search mode is enabled by default starting with Windows XP with Service Pack 2 (SP2).

If SafeDllSearchMode is enabled, the search order is as follows:

The directory from which the application loaded.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory. GetCurrentDirctory();
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. The App Paths key is not used when computing the DLL search path.
If SafeDllSearchMode is disabled, the search order is as follows:

The directory from which the application loaded.
The current directory.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this 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. The App Paths key is not used when computing the DLL search path.
注:上面说到通过注册表开启是指将HKLM\System\CurrentControlSet\Control\Session Manager键值下的属性SafeDllSearchMode的值设置为1(如果没有SafeDllSearchMode就自己手动创建)。
一般默认没有开启。
在安全DLL搜索模式开启的情况下,搜索顺序是:
1、应用程序EXE所在的路径。
2、系统目录。
3、16位系统目录 (c:\windows\system)
4、Windows目录
5、当前目录
6、PATH环境变量指定的目录

 如果安全DLL搜索模式不支持或者被禁用,那么搜索顺序是:
       1、应用程序EXE所在的路径。
       2、当前目录
       3、系统目录。
       4、16位系统目录
       5、Windows目录
       6、PATH环境变量指定的目录

系统目录用GetSystemDirectory获取,windows目录用GetWindowsDirectory,当前目录用GetCurrentDirctory。当前目录和exe所在目录一般相同,但是调试程序时是不同的。

 int main()
 {
 TCHAR path[MAX_PATH];
 GetSystemDirectory(path,MAX_PATH);
 cout<<path<<endl;// c:\windows\system32
 GetWindowsDirectory(path,MAX_PATH);
 cout<<path<<endl;// c:\windows
 GetCurrentDirctory(MAX_PATH,path);
 cout<<path<<endl;
 getchar();
 return 0;
}

32位系统:
复制x86的dll到C:\Windows\System32 这个很容器理解。

64位系统:
**复制x64的dll文件到C:\Windows\System32
复制x86的dll文件到C:\Windows\SysWOW64**

你把64位的dll放在SysWOW64目录下,会找不到dll。必须放在System32才可找到。
而你把32位的dll放在System32目录下,也会找不到dll。必须放在SysWOW64才可找到。
放在SysWOW64,打印出来的dllpath仍然是system32下的,这个是重定向的结果。

总结,安全dll搜索:
应用程序EXE所在的路径 > c:\windows\system32 > c:\windows > 当前目录 > path 目录。
无安全dll搜搜:
应用程序EXE所在的路径 >当前目录> c:\windows\system32 > c:\windows > path 目录。

为了防止dll劫持。
验证也很简单,写一个应用程序,写一个dll 打印路径(Dllmain:GetModuleFileName),把dll放在上面的2个目录,然后运行程序,看打印的是哪个路径,就知道这两个哪个优先了。多尝试几个即可。

//
1.
WoW64子系统是一个轻量级的compatibility layer, 在所有版本的windows上都拥有同样的接口. 它的主要目的是用来创建32-bit环境, 为了让32位的应用程序可以不经过任何修改就运行在64-bit的系统上, 它提供了必须的接口.
技术上说, WOW64是由三个DLL实现的.
Wow64.dll 是Windows NT kernel的核心接口, 在32位和64位调用之间进行转换, 包括指针和调用栈的操控. Wow64win.dll 为32位应用程序提供合适的入口指针. Wow64cpu.dll 负责将处理器在32位和64位的模式之间转换。
注册表和文件系统
WOW子系统也会处理运行64-bit应用程序的其他关键方面. 比如说, 在管理32位应用程序与windows注册表的交互时, 会给存储子系统提供接口(32位的注册表与64位的注册表不太一样.) 操作系统使*

用%SystemRoot%\system32目录来存储64-bit的库文件和可执行文件. 这样做是为了向后兼容, 因为很多旧系统的应用程序都是使用hardcoded的方式来获取这个路径的. 当执行32位应用程序的时候, WOW64会将对DLL的请求从system32重定向到%SystemRoot%\SysWOW64, 在SysWOW64目录中, 包含了旧系统的库和可执行文件.* (故意搞反:32位dll在SysWOW64里,64位dll在system32里; 32位程序访问system 路径本来应该是system32,但是会重定向到SysWOW64内;64位程序由操作系统支持在system32里查找dll;)

2.
64bit操作系统的重定向机制以及目的
在64bit操作系统中,为了无缝兼容32bit程序的运行,64bit的Windows操作系统采用重定向机制。目的是为了能让32bit程序在 64bit的操作系统不仅能操作关键文件文夹和关键的注册表并且又要避免与64bit程序冲突。
微软采用重定向机制的原理很简单,说白了就是 让关键文件/文件夹或者关键注册表有2个副本。 1个副本是给32bit程序访问,一个副本给64bit程序访问。
64bit操作系统是如何控制32bit和64bit程序访问对应各自的副本?
这个问题,就是重定向机制的核心功能。

例子: 32bit程序在64bit Windows操作系统,要访问system32目录。
在正常的情况下: 64bit Windows操作系统的重定向机制 会在内部 把 system32目录 转向 syswow64目录, 因此32bit程序对system32目录的操作,实际是对syswow64目录进行操作。
以为由于重定向的干预,因为可以这么认为 system32目录是供给64bit程序使用的,而syswow64目录是给32bit程序使用的
代码例子: 32bit程序中有一个代码
deletefile(‘c:/windows/system32/a.txt’) ;
这个代码在64bit Windows操作系统中,它只会删除syswow64目录中的a.txt文件,而不会删除system32目录的a.txt文件。 这就是重定向的干预结果。

  1. 那32bit程序要真正访问64bit程序的system32目录要如何做呢?
    微软提供一套API,可以做到上面的要求。 通过 Wow64DisableWow64FsRedirection 和 Wow64RevertWow64FsRedirection API 来配合使用

    代码例子: 32bit程序中有一个代码
    Wow64DisableWow64FsRedirection // 关闭重定向
    deletefile(‘c:/windows/system32/a.txt’) ;
    Wow64RevertWow64FsRedirection // 恢复重定向
    以上代码,就有效的删除了system32目录里面的a.txt文件,而不是syswow64目录中的a.txt文件。

    1. #if defined(_WIN64) 判断系统是否是64位
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值