如何在 Windows 进程 (XP / Windows 7) 中找到加载的 DLL 的实际路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6705531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find actual path to a loaded DLL with in a windows process (XP /Windows 7)
提问by Jayan
We create a DLL for other applications to load and use some of the functionality in the application. The DLL has dependency on the the actual path where it is loaded from.
我们为其他应用程序创建一个 DLL 以加载和使用应用程序中的某些功能。DLL 依赖于加载它的实际路径。
<product_home>/bin/<DLL is here>
|
|----/configdir/configfile
|----/lib/<java jarfiles>
It needs the product_home location to read config files and load jar files etc
它需要 product_home 位置来读取配置文件和加载 jar 文件等
My windows application proloads a special DLL. I need to find actual path to a loaded DLL with in the process and use it to set a "HOME" variable. This will be used in rest of the processing. Using an externally set environment variable some time fails when there are multiple versions of dll present on the machine. To me it looks like DLL can figure out its own "product_home" as long it can get the actual loaded location.
我的 Windows 应用程序 proloads 一个特殊的 DLL。我需要在进程中找到加载的 DLL 的实际路径,并使用它来设置“HOME”变量。这将用于其余的处理。当机器上存在多个版本的 dll 时,使用外部设置的环境变量有时会失败。对我来说,看起来 DLL 可以找出自己的“product_home”,只要它可以获得实际加载的位置。
The DLL This article Get Your DLL's Path/Nameprovides one such way- (yet to try it successfully. The generated exe crashes). Is this the correct approach?
The DLL This article Get Your DLL's Path/Name提供了一种这样的方法-(尚未成功尝试。生成的exe崩溃)。这是正确的方法吗?
回答by eran
Either I don't understand your need, or the link you mention is not what you need. If I understand you correctly, you'd like to get the full path of a certain DLL loaded by the process. So, say that DLL is "kernel32.dll", you'd like to get "c:\windows\system32\kernel32.dll". Please correct me if I'm wrong.
要么我不明白您的需求,要么您提到的链接不是您需要的。如果我理解正确,您希望获得进程加载的某个 DLL 的完整路径。所以,假设 DLL 是“kernel32.dll”,你想要得到“c:\windows\system32\kernel32.dll”。如果我错了,请纠正我。
If that's what you want, the easiest way to do that would be:
如果这就是您想要的,那么最简单的方法是:
HMODULE hModule = GetModuleHandle(_T("kernel32.dll"));
TCHAR dllPath[_MAX_PATH];
GetModuleFileName(hModule, dllPath, _MAX_PATH);
Failures checks omitted for brevity - read more about GetModuleHandleand GetModuleFileName.
为简洁起见省略了失败检查 - 阅读有关GetModuleHandle和GetModuleFileName 的更多信息。