是否可以从批处理文件或cmd脚本将目录添加到DLL搜索路径?
MSDN 说函数 SetDllDirectory() 可用于插入目录进入 DLL 搜索路径。 可以从批处理文件或cmd脚本访问此函数,也许可以通过cscript?
目的是让我们的 dll 开发版本先于 %WINDIR% 等中预先存在的旧版本找到,而不必为此编写程序。
MSDN says that the function SetDllDirectory() can be used to insert a directory into the DLL Search Path. Can this function be accessed from a batch file or cmd script, perhaps using via cscript?
The aim is to have our development version of a dll found before a pre-existing older one in %WINDIR% etc. without having to write a program just for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将 DLL 放置在与可执行文件相同的路径中,在
%WINDIR%
之前先搜索可执行文件。 无法直接从批处理文件调用SetDllDirectory
。但是,您可以将 DLL 目录插入到
%PATH%
变量中,Windows 将在那里找到 DLL。You can place the DLL in the same path as the executable, which is searched first before
%WINDIR%
. There's no way to callSetDllDirectory
from a batch file directly.But, you can insert your DLL directory in the
%PATH%
variable, and Windows will then find the DLL there.如果 DLL 与可执行文件不在同一文件夹中,Windows 将在系统路径中指定的文件夹中搜索该文件。 因此,您所需要做的就是将文件夹放在路径的开头。
您可以使用以下批处理命令来执行此操作:
如果您的路径包含空格,则需要使用以下批处理命令:
但请记住,此路径更改仅对当前控制台会话的 PATH 进行。 如果关闭并重新打开控制台,这些路径更改将会丢失。
If the DLL is not in the same folder as the executable Windows will search for the file in the folders specified in the system path. So all you need to do is put your folder at the start of the path.
You can do this using the following batch command:
If your path contains white space you need to use the following batch command:
But remember this path change is only made to the PATH of the current console session. If you close and reopen the console these path changes will be lost.
为了消除有关 dll 搜索顺序的争议(在 @jussij 答案的评论中),以下是来自 Microsoft 文档的列表:
如果启用了
SafeDllSearchMode
,则搜索顺序如下:GetSystemDirectory
函数获取该目录的路径。GetWindowsDirectory
函数获取该目录的路径。
如果禁用
SafeDllSearchMode
,则搜索顺序如下:GetSystemDirectory
函数获取该目录的路径。GetWindowsDirectory
函数获取该目录的路径。请参阅 http://msdn .microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx#standard_search_order_for_desktop_applications
To clear up dispute on the dll search order (in the comments on @jussij's answer), here's the list, drawn from Microsoft's doc:
If
SafeDllSearchMode
is enabled, the search order is as follows:GetSystemDirectory
function to get the path of this directory.GetWindowsDirectory
function to get thepath of this directory.
If
SafeDllSearchMode
is disabled, the search order is as follows:GetSystemDirectory
function to get the path of this directory.GetWindowsDirectory
function to get the path of this directory.See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx#standard_search_order_for_desktop_applications