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?
MSDN说,函数SetDllDirectory()可用于将目录插入DLL搜索路径。可以从批处理文件或cmd脚本访问此函数,也许使用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.
目的是让我们的开发版本的dll在%WINDIR%之前的旧版本之前找到,而不必为此编写程序。
Thanks in advance for your time and thoughts.
提前感谢您的时间和想法。
3 个解决方案
#1
You can place the DLL in the same path as the executable, which is searched first before %WINDIR%. There's no way to call SetDllDirectory from a batch file directly.
您可以将DLL放在与可执行文件相同的路径中,该文件在%WINDIR%之前首先搜索。无法直接从批处理文件中调用SetDllDirectory。
But, you can insert your DLL directory in the %PATH% variable, and Windows will then find the DLL there.
但是,您可以在%PATH%变量中插入DLL目录,然后Windows将在那里找到DLL。
set PATH=C:\path to your dll;%PATH%
#2
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.
目的是让我们的开发版本的dll在%WINDIR%之前的旧版本之前找到,而不必为此编写程序。
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.
如果DLL与可执行文件不在同一文件夹中,则Windows将在系统路径中指定的文件夹中搜索该文件。所以你需要做的就是把你的文件夹放在路径的开头。
You can do this using the following batch command:
您可以使用以下批处理命令执行此操作:
set PATH=c:\MyDLLFolder;%PATH%
If your path contains white space you need to use the following batch command:
如果路径包含空格,则需要使用以下批处理命令:
set PATH="C:\My DLL Folder";%PATH%
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.
但请记住,此路径更改仅针对当前控制台会话的PATH。如果关闭并重新打开控制台,则这些路径更改将丢失。
#3
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:
要清除dll搜索顺序上的争议(在@ jussij的答案的评论中),这是从Microsoft的文档中提取的列表:
If SafeDllSearchMode
is enabled, the search order is as follows:
如果启用了SafeDllSearchMode,则搜索顺序如下:
- 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.
- 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.
加载应用程序的目录。
系统目录。使用GetSystemDirectory函数获取此目录的路径。
16位系统目录。没有函数可以获取此目录的路径,但会搜索它。
Windows目录。使用GetWindowsDirectory函数获取此目录的路径。
当前目录。
PATH环境变量中列出的目录。请注意,这不包括App Paths注册表项指定的每个应用程序路径。计算DLL搜索路径时不使用App Paths键。
If SafeDllSearchMode
is disabled, the search order is as follows:
如果禁用SafeDllSearchMode,则搜索顺序如下:
- 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.
加载应用程序的目录。
当前目录。
系统目录。使用GetSystemDirectory函数获取此目录的路径。
16位系统目录。没有函数可以获取此目录的路径,但会搜索它。
Windows目录。使用GetWindowsDirectory函数获取此目录的路径。
PATH环境变量中列出的目录。请注意,这不包括App Paths注册表项指定的每个应用程序路径。计算DLL搜索路径时不使用App Paths键。
#1
You can place the DLL in the same path as the executable, which is searched first before %WINDIR%. There's no way to call SetDllDirectory from a batch file directly.
您可以将DLL放在与可执行文件相同的路径中,该文件在%WINDIR%之前首先搜索。无法直接从批处理文件中调用SetDllDirectory。
But, you can insert your DLL directory in the %PATH% variable, and Windows will then find the DLL there.
但是,您可以在%PATH%变量中插入DLL目录,然后Windows将在那里找到DLL。
set PATH=C:\path to your dll;%PATH%
#2
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.
目的是让我们的开发版本的dll在%WINDIR%之前的旧版本之前找到,而不必为此编写程序。
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.
如果DLL与可执行文件不在同一文件夹中,则Windows将在系统路径中指定的文件夹中搜索该文件。所以你需要做的就是把你的文件夹放在路径的开头。
You can do this using the following batch command:
您可以使用以下批处理命令执行此操作:
set PATH=c:\MyDLLFolder;%PATH%
If your path contains white space you need to use the following batch command:
如果路径包含空格,则需要使用以下批处理命令:
set PATH="C:\My DLL Folder";%PATH%
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.
但请记住,此路径更改仅针对当前控制台会话的PATH。如果关闭并重新打开控制台,则这些路径更改将丢失。
#3
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:
要清除dll搜索顺序上的争议(在@ jussij的答案的评论中),这是从Microsoft的文档中提取的列表:
If SafeDllSearchMode
is enabled, the search order is as follows:
如果启用了SafeDllSearchMode,则搜索顺序如下:
- 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.
- 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.
加载应用程序的目录。
系统目录。使用GetSystemDirectory函数获取此目录的路径。
16位系统目录。没有函数可以获取此目录的路径,但会搜索它。
Windows目录。使用GetWindowsDirectory函数获取此目录的路径。
当前目录。
PATH环境变量中列出的目录。请注意,这不包括App Paths注册表项指定的每个应用程序路径。计算DLL搜索路径时不使用App Paths键。
If SafeDllSearchMode
is disabled, the search order is as follows:
如果禁用SafeDllSearchMode,则搜索顺序如下:
- 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.
加载应用程序的目录。
当前目录。
系统目录。使用GetSystemDirectory函数获取此目录的路径。
16位系统目录。没有函数可以获取此目录的路径,但会搜索它。
Windows目录。使用GetWindowsDirectory函数获取此目录的路径。
PATH环境变量中列出的目录。请注意,这不包括App Paths注册表项指定的每个应用程序路径。计算DLL搜索路径时不使用App Paths键。