LoadLibrary是否解析%windir%等环境变量
如果我这样做 LoadLibrary("%windir%\\system32\\ole32.dll")
是否意味着 Windows 将仅从“c:\windows\system32\ole32”加载.dll”? C 中的 LoadLibrary() 是否理解环境变量?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Serge 所说并经过仔细测试,LoadLibrary 不会在路径中进行环境变量替换。
但是,Windows API中有一个函数可以替换字符串中的环境变量:
ExpandEnvironmentStrings()
。您可以在调用LoadLibrary()
之前对路径执行所需的替换。as Serge said and carefully tested, LoadLibrary does not do environment variable substitution in path.
however, there is a function in the windows API to replace environment variables in strings:
ExpandEnvironmentStrings()
. you can perform the required substitution on your path before callingLoadLibrary()
.LoadLibrary 文档明确指出:
也就是说,他们没有提到对环境变量替换的支持。我严重怀疑它们是否支持环境变量替换:这是一项 shell 功能,而不是内核 API 功能。
顺便说一句,这意味着
LoadLibrary()
会将%windir%\blah.dll
视为相对路径,因为它不以驱动器号或 UNC 路径开头。因此,它会遍历整个目录系列,寻找名为%windir%
的子目录,但它不太可能找到!我快速测试了一下:它证实了我的观点。错误 = 126:找不到指定的模块。
The docs for LoadLibrary clearly state that:
That said, they don't mention support for environment variables substitution. I seriously doubt they do support environment variables substitution: That's a shell feature, not a kernel API one.
BTW, that means
LoadLibrary()
would consider%windir%\blah.dll
as a relative path since it doesn't start with a drive letter or a UNC path. Hence it would look through the whole series of directories, looking for a subdir named%windir%
, which it's not likely to find!I gave it a quick test: It confirms my opinion. Error = 126 : The specified module could not be found.