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
I assume you’ve got a process handle, or id of the process your
my.dllis 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 ):MODULEENTRY32has a field calledszModulewhich 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 (th32ProcessIDofPROCESSENTRY32).Then you’ll iterate on all modules of the snapshot using
Module32FirstandModule32Next. 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)