Reputation: 1054
A common method foo() is defined in two DLLs A.dll and B.dll. Now when a process proc.exe loads both DLLs and call foo() method simultaneously from two threads. is there any way to know foo() was loaded from which DLL A.dll or B.dll at run time. I need this information for logging purpose. I couldn't find anything relevant on internet.
GetModuleFileName() would return the process name proc.exe not the Dlls name.
Upvotes: 1
Views: 317
Reputation: 5812
Assuming that you have the address of the function you should be able to use the following to determine the base address of the module.
HMODULE ModuleFromAddress(void *address)
{
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQuery(address, &mbi, sizeof(mbi)) != 0)
return (HMODULE)mbi.AllocationBase;
return NULL;
}
Then feed the result of that into GetModuleFileName
Upvotes: 3
Reputation: 10828
You could find out the address where foo is located and the address range where each dll is loaded. Foo must be in one of the 2 address ranges. For finding out where a dll is loaded, check Finding the memory address of a loaded DLL in a process in C++. The MODULEINFO structure mentioned in the link provides the start address & the size.
Upvotes: 1
Reputation: 406
Doesn't the callstack (atleast in MS Visual Studio) tell exactly that? (Sorry can't write comments due to reputation limit)
Upvotes: 1