none
Get function caller module handle RRS feed

  • Question

  • I need to find what module is calling a function inside a dll from withing this function.

    We have a dll that is used for logging. This dll allows a shared file handle between dll's using it.

    Its a bit complicated to explain, but il just try :)

    A.DLL use LOG.DLL and initialize the log if not initialized (%APPDATA%\A_DLL\Log.txt)
    Test.exe use LOG.DLL and initialize the log if not initialized (%APPDATA%\TEST_APP\Log.txt)

    Now Test.exe start using A.DLL
    When A.DLL is loaded, it will find a valid log handle (initialized by Test.exe) and will use this for logging. So the dll will log everything into the application log.

    Note that only the LOG.DLL is linked to the project using the lib / header file. All other dll's are loaded trough LoadLibrary()


    Now we want to add an extra feature to make review the logs easier.
    As Test.exe and A.DLL write each log to the same file, its sometime hard to see what line in the log comes from which module.

    So we want to add the module name to it. For this we need to find out what module is calling the WriteLogLine() in the LOG.DLL

    Is this possible and how can we do this without having to pass a parementer with module name all the time when calling log.
    Note that not all dll's and applications that use the log.dll are written in visual studio. It has to support both VS and Borland :(
    Wednesday, January 27, 2010 3:07 PM

Answers

  • First check if _ReturnAddress intrinsic [http://msdn.microsoft.com/en-us/library/64ez38eh.aspx] or _AddressOfReturnAddress function can be used for determining the address of the caller.

     

    Having an address from caller module, retrieve the HMODULE using this fragment:

     

        HMODULE hModule;

        ::GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)caller_address, &hModule);

     

    Alternatively arrange a logger helper which keeps the handle of the client.

    • Marked as answer by Nightmare_BE Thursday, January 28, 2010 8:52 AM
    Wednesday, January 27, 2010 3:33 PM

All replies

  • The quickest and simplest method is to use GetModuleFileName(http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx) with NULL for the HMODULE parameter.
    This doesn't work too well if a DLL is calling a function from inside another DLL though -_-;
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    • Proposed as answer by Ali Rafiee Wednesday, January 27, 2010 3:26 PM
    Wednesday, January 27, 2010 3:11 PM
  • First check if _ReturnAddress intrinsic [http://msdn.microsoft.com/en-us/library/64ez38eh.aspx] or _AddressOfReturnAddress function can be used for determining the address of the caller.

     

    Having an address from caller module, retrieve the HMODULE using this fragment:

     

        HMODULE hModule;

        ::GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)caller_address, &hModule);

     

    Alternatively arrange a logger helper which keeps the handle of the client.

    • Marked as answer by Nightmare_BE Thursday, January 28, 2010 8:52 AM
    Wednesday, January 27, 2010 3:33 PM