c++pointerswinapidllgetprocaddress

Get name of object imported from DLL


I have a pointer to some object that was loaded from DLL using GetProcAddress:

CSomeClass* pSomeClass;
pSomeClass = (CSomeClass*)GetProcAddress(someDLLinstance, "SomeUnknownName");

I cannot modify the code above, but I need to obtain "SomeUnknownName" string after it has gone out of scope. All I can access is pSomeClass pointer. Is there any convenient way I can get the imported object's name from its pointer? Right now I just dump all the export names from DLL, then use GetProcAddress() on each of them to get all pointers to all exported objects/functions, and then compare pSomeClass to those pointers, but it appears to be very slow for a solution.


Solution

  • If you really need to do this, you can use the DbgHelp functions to do the job. Specifically, you'd be looking at SymFromAddr in this case.

    You start by calling SymInitialize, then you can call SymFromAddr. You give it the address of a SYMBOL_INFO structure. You fill in the address and maximum symbol length, and it'll return the name.

    When you're done, you're at least supposed to call SymCleanup to let the symbol manager library shut down, release any memory it's holding, etc. Presumably that would/will all happen when your process ends, but it's cleaner to call it when you're done.

    If you have debug information, that will succeed for pretty much any symbol. Even without debug information, however, it will (at least normally) succeed for exported symbols.