-1

I have the deplorable mission the access a global(!) variable of a .exe from within my .dll (loaded by that .exe).

A header file gives me following declaration of the global variable:

extern Element *ops_TheActiveElement; 

Basically, the global variable is a pointer to a class instance which i need to acess.

From what I found on the web, i could use something like this from within the dll:

Element* getTheActiveElement()
{

auto handle = GetModuleHandle(NULL);

if (handle == NULL)
    std::cout << "handle" << handle << std::endl;

auto activeElement = GetProcAddress(handle, "ops_TheActiveElement") ;

return (Element*)activeElement;
}

Getting the module handle is fine, but getting the address of the global variable fails. I am not sure, if this is possible at all since i am not familiar with Windows programming. What could be a possible approach? Thank you in advance!

mneuner
  • 433
  • 5
  • 25
  • You can check out exported symbols with `dumpbin /exports`. If that variable is not exported, then you cannot use `GetProcAddress` to get it (usually an exe doesn't export anything). Of course, you can find that variable using a disassembler and some reverse engineering work. – geza Nov 28 '18 at 10:55

1 Answers1

0
  • Proffered approach would be to supply this variable explicitly when invoking code from your library. Use of extern (except for extern "C" functions) and use of global variables are a code smell.
  • Alternatively you can export this variable from executable and link you dll to executable so ops_TheActiveElement can be accessed directly.
user7860670
  • 35,849
  • 4
  • 58
  • 84