如何调整%PATH%用于动态加载的本机DLL? [英] How to adjust %PATH% for dynamically loaded native dlls?

查看:710
本文介绍了如何调整%PATH%用于动态加载的本机DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态加载.NET程序集是依赖于的本机的.dll文件的几个串 位于不同的文件夹。但是,Windows发现这些DLL仅当它们的文件夹中,当我启动应用程序的PATH环境变量中。

I'm loading dynamically a .NET assembly that depends on several bunches of native .dlls located in various folders. But Windows finds those DLLs only if their folders are in the PATH environment variable when my application is started.

我想从我的程序修改我的PATH变量,使找到所需的库。 根据 MSDN 搜索顺序如下:... 中列出的PATH环境变量的目录。

I would like to modify my PATH variable from my program to allow finding the necessary libraries. According to MSDN "the search order is as follows: ... The directories that are listed in the PATH environment variable."

PATH环境变量的哪个实例使用?

Which instance of the PATH environment variable is used?

每个进程都有一个实例。
我试过 Environment.SetEnvironmentVariable(PATH,...),但它并没有帮助。 我也试过 SetDefaultDllDirectories() 和...一起 AddDllDirectory() 但这些要么没有区别。

Every process has an instance.
I tried Environment.SetEnvironmentVariable("PATH", ...) but it did not help. I also tried SetDefaultDllDirectories() together with AddDllDirectory() but these made no difference either.

的症状是,当%PATH%开始我的.EXE时包含了必要的文件夹 (从CMD提示 - 这是一个控制台应用程序),的ProcessMonitor显示,本机.dll文件被探测的所有路径文件夹中,并最终发现

The symptom is that when %PATH% contains the necessary folders when starting my .exe (from a CMD prompt – it is a console application), ProcessMonitor shows that the native .dlls are probed in all the PATH folders, and are eventually found.

但是,当%PATH%不包含必要的文件夹,在开始的时候,那么本机.dll文件都在探讨中的.exe文件夹,并在SYSTEM32只(虽然%PATH%含有更多), 无论上述SetEnvironmentVariable方法()/ SetDefaultDllDirectories()的/ AddDllDirectory()调用。

But when %PATH% does not contain the necessary folders at the time of starting, then the native .dlls are probed in the .exe folder and in SYSTEM32 only (although %PATH% contains far more), regardless of the above-mentioned SetEnvironmentVariable()/SetDefaultDllDirectories()/AddDllDirectory() calls.

这是怎么回事吗?我究竟做错了什么?为什么我不能调整路径为我处理​​的有效的?

What is going here? What am I doing wrong? Why I cannot adjust the PATH for my process effectively?

注:AppDomain.AssemblyResolve事件不能帮助我,因为它没有被解雇本地时.dll文件 加载其他本地.DLL文件。

Note: The AppDomain.AssemblyResolve event cannot help me because it's not fired when native .dlls load other native .dlls.

推荐答案

这是因为每个进程继承从它衍生的过程中它的环境。和它不发生的DIT在微软的东西像路径可能执行的过程中改变,所以在CLR从未过程执行期间刷新环境(并没有规定的处理这样做本身的装置)。请参阅<一href="http://social.msdn.microsoft.com/Forums/vstudio/en-US/acf2d0f3-143e-4ba5-acdc-76a70a5c9830/environment-variables-refresh?forum=csharpgeneral" rel="nofollow">http://social.msdn.microsoft.com/Forums/vstudio/en-US/acf2d0f3-143e-4ba5-acdc-76a70a5c9830/environment-variables-refresh?forum=csharpgeneral了解详细信息。

That is because each process inherits its environment from the process that spawned it. And it didn't occur to the dit at Microsoft that something like PATH might change during the course of execution, so the CLR never refreshes the environment during process execution (and doesn't provide a means for the process to do so itself). See http://social.msdn.microsoft.com/Forums/vstudio/en-US/acf2d0f3-143e-4ba5-acdc-76a70a5c9830/environment-variables-refresh?forum=csharpgeneral for details.

由于加载器解析引用到通过正常的Win32方式非托管的DLL,你应该看P /调用这些Win32函数来改变使用由Win32的 调用LoadLibrary() LoadLibraryEx()

Since the loader is resolving references to unmanaged DLLs via the normal Win32 way, you should probably look at P/Invoking these Win32 functions to alter the DLL search order used by the Win32 LoadLibrary() and LoadLibraryEx():

添加一个目录,用于定位的DLL应用程序的搜索路径。

Adds a directory to the search path used to locate DLLs for the application.

AddDllDirectory()

添加一个目录的过程DLL搜索路径。

Adds a directory to the process DLL search path.

SetDefaultDllDirectories()

指定默认设置的目录时,调用进程加载DLL搜索。

Specifies a default set of directories to search when the calling process loads a DLL.

RemoveDllDirectory()

移除加入到该过程中DLL搜索路径通过使用AddDllDirectory一个目录

Removes a directory that was added to the process DLL search path by using AddDllDirectory.

参见 DLL搜索订单

这篇关于如何调整%PATH%用于动态加载的本机DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆