0

I have a main application EXE "main.EXE". I've created a global pointer in a header file globalf.h with extern like:

globalf.h

extern SomeClass* cPtr;

Now in Main.EXE code

#include globalf.h
INitialize()
{
cPtr = new SomeClass(); // works fine.

D.DLL:

#include "globalf.h"
void DllFunc()
{
cPtr->someFunction1(); // link error unreslved external symbol.

I'm not able to access the global pointer variable in DLL code, even though I had declared inMain.EXE code.

I know this declaration in main.EXE's Initialize() isn't visible to DLL. But how do I achieve this? Is there a way in C++ for handling such globals across DLL & EXEs.

Chinmay jain
  • 979
  • 1
  • 9
  • 20
codeLover
  • 3,720
  • 10
  • 65
  • 121
  • 1
    You should probably read [What happens to global and static variables in a shared library when it is dynamically linked?](https://stackoverflow.com/questions/19373061/what-happens-to-global-and-static-variables-in-a-shared-library-when-it-is-dynam). Maybe [Sharing a global/static variable between a process and DLL](https://stackoverflow.com/questions/4911994/sharing-a-global-static-variable-between-a-process-and-dll) as well. Or [using global variable in dll and exe](https://stackoverflow.com/questions/14197237/using-global-variable-in-dll-and-exe). – Some programmer dude Aug 03 '17 at 10:51
  • Or just about any link provided by searching for [`exe dll shared global variable`](https://www.google.se/search?q=exe+dll+shared+global+variable). – Some programmer dude Aug 03 '17 at 10:54

1 Answers1

0

Instead of exporting a global pointer variable, I'd pack it and other variables you need to share between the EXE and the DLL in a structure, and then pass a pointer to that structure as a function parameter for example to a DLL initialization function.

// Function exported from the DLL.
// Called by the EXE during initialization.
//
// The DLL receives a pointer to the global state structure,
// shared between the EXE and the DLL.
//
BOOL YourDllInit(GlobalStuff* pGlobal);
Mr.C64
  • 41,637
  • 14
  • 86
  • 162