是否可以从批处理文件或cmd脚本将目录添加到DLL搜索路径?

发布于 2024-07-14 00:14:26 字数 418 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无边思念无边月 2024-07-21 00:14:26

您可以将 DLL 放置在与可执行文件相同的路径中,在 %WINDIR% 之前先搜索可执行文件。 无法直接从批处理文件调用 SetDllDirectory

但是,您可以将 DLL 目录插入到 %PATH% 变量中,Windows 将在那里找到 DLL。

set PATH=C:\path to your dll;%PATH%

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.

But, you can insert your DLL directory in the %PATH% variable, and Windows will then find the DLL there.

set PATH=C:\path to your dll;%PATH%
末蓝 2024-07-21 00:14:26

目的是为了我们的发展
在 a 之前找到的 dll 版本
%WINDIR% 中已有旧的
等等,无需编写程序
就是为了这个。

如果 DLL 与可执行文件不在同一文件夹中,Windows 将在系统路径中指定的文件夹中搜索该文件。 因此,您所需要做的就是将文件夹放在路径的开头。

您可以使用以下批处理命令来执行此操作:

 set PATH=c:\MyDLLFolder;%PATH%

如果您的路径包含空格,则需要使用以下批处理命令:

 set PATH="C:\My DLL Folder";%PATH%

但请记住,此路径更改仅对当前控制台会话的 PATH 进行。 如果关闭并重新打开控制台,这些路径更改将会丢失。

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.

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:

 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.

漆黑的白昼 2024-07-21 00:14:26

为了消除有关 dll 搜索顺序的争议(在 @jussij 答案的评论中),以下是来自 Microsoft 文档的列表:

如果启用了 SafeDllSearchMode,则搜索顺序如下:

  1. 应用程序加载的目录。
  2. 系统目录。 使用GetSystemDirectory函数获取该目录的路径。
  3. 16位系统目录。 没有函数获取该目录的路径,但是会搜索。
  4. Windows 目录。 使用GetWindowsDirectory函数获取
    该目录的路径。
  5. 当前目录。
  6. PATH 环境变量中列出的目录。 请注意,这不包括应用程序路径注册表项指定的每个应用程序路径。 计算 DLL 搜索路径时不使用 App Paths 键。

如果禁用SafeDllSearchMode,则搜索顺序如下:

  1. 加载应用程序的目录。
  2. 当前目录。
  3. 系统目录。 使用GetSystemDirectory函数获取该目录的路径。
  4. 16位系统目录。 没有函数获取这个目录的路径,但是会搜索。
  5. Windows 目录。 使用GetWindowsDirectory函数获取该目录的路径。
  6. PATH 环境变量中列出的目录。 请注意,这不包括应用程序路径注册表项指定的每个应用程序路径。 计算 DLL 搜索路径时不使用 App Paths 键。

请参阅 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:

  1. The directory from which the application loaded.
  2. The system directory. Use the GetSystemDirectory function to get the path of this directory.
  3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  4. The Windows directory. Use the GetWindowsDirectory function to get the
    path of this directory.
  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. The App Paths key is not used when computing the DLL search path.

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

  1. The directory from which the application loaded.
  2. The current directory.
  3. The system directory. Use the GetSystemDirectory function to get the path of this directory.
  4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  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. 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.

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx#standard_search_order_for_desktop_applications

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文