如何确定加载我的 DLL 的 DLL 名称(字符串)?
我正在编写一个由第三方驱动程序加载的设备驱动程序。我需要一种方法来确定正在加载我的设备驱动程序的第 3 方驱动程序的名称(用于调试目的)。
例如,GetModuleFileName
将为我提供可执行文件的名称。我希望能够获取 DLL 名称。
堆栈跟踪可能是以下之一:
(a)
app0.exe
abc.dll <- detect "abc"
common.dll
my.dll
(b)
app1.exe
xyz.dll <- detect "xyz"
common.dll
my.dll
(c)
app2.exe
common.dll
my.dll
ps - 我只需要 C++ \ Windows 的方法
I'm writing a device driver that is loaded by a 3rd-party driver. I need a way to determine the name of the 3rd-party driver that is loading my device driver (for debug purposes).
For example, GetModuleFileName
will provide me the name of the executable. I'd like instead to be able to get the DLL names.
The stack trace may be one of the following:
(a)
app0.exe
abc.dll <- detect "abc"
common.dll
my.dll
(b)
app1.exe
xyz.dll <- detect "xyz"
common.dll
my.dll
(c)
app2.exe
common.dll
my.dll
p.s. - I only need a method for C++ \ Windows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您有一个进程句柄,或者加载
my.dll
的进程 ID。请参阅 http://msdn.microsoft.com/en-us/library/ms686701(v=VS.85).aspx 它将拍摄进程的快照并提供所有信息。
有趣的方法是
BOOL ListProcessModules( DWORD dwPID )
:MODULEENTRY32
有一个名为szModule
的字段,其中包含模块的名称。请参阅 http://msdn.microsoft.com/en- us/library/ms684225(VS.85).aspx可以使用
CreateToolhelp32Snapshot
从进程中检索所有模块条目,这需要进程 ID(th32ProcessID
PROCESSENTRY32
)。然后,您将使用
Module32First
和Module32Next
迭代快照的所有模块。另外,不要忘记关闭 CreateToolhelp32Snapshot 给出的句柄。(注意:这些方法可从 kernel32.dll 获得)
这称为“模块行走”,更多信息请参见:http://msdn.microsoft.com/en-us/library/ms684236(v=VS.85).aspx(描述这个答案已经包含了什么)
I assume you've got a process handle, or id of the process your
my.dll
is loaded in.See an MSDN example at http://msdn.microsoft.com/en-us/library/ms686701(v=VS.85).aspx which will take a snapshot of a process and give all information.
The interesting method is at
BOOL ListProcessModules( DWORD dwPID )
:MODULEENTRY32
has a field calledszModule
which contains the name of the module. See http://msdn.microsoft.com/en-us/library/ms684225(VS.85).aspxAll module entries can be retrieved from a process using
CreateToolhelp32Snapshot
, which requires the process id (th32ProcessID
ofPROCESSENTRY32
).Then you'll iterate on all modules of the snapshot using
Module32First
andModule32Next
. Also, do not forget to close the handle given byCreateToolhelp32Snapshot
.(Note: these methods are available from kernel32.dll)
This is called Module Walking, more on here: http://msdn.microsoft.com/en-us/library/ms684236(v=VS.85).aspx (described what is in this answer already)
如果仅用于调试目的,您可以执行 stackwalk
请参阅此 stackoverflow 详细回答
If it's for debug purposes only, you can just do a stackwalk
See this stackoverflow answer for details