Click to See Complete Forum and Search --> : Global variable from exe use by dll loaded by the exe


stephenteh
December 1st, 2006, 06:42 AM
Hi,

I have a question global variable sharing (I'm a beginner of C++). I have 2 projects, one is exe another is dll. The dll will be loaded by the exe. I have a global variable in the exe let say variable A, is it possible for the dll to use this variable? I cannot use the extern as it's in difference project. I think the dll should be able to use this variable as the dll is loaded by the exe that's mean there are in the same process space or same memory space. I just need a way to let the dll know there is such variable in the exe project.

btw, the dll will be dynamically loaded by the exe not statically linked during compile...

Thanks in advance

Zaccheus
December 1st, 2006, 07:27 AM
Having global variables should usually be avoided if possible.

To answer your question: You could put the global variable into the DLL and export the global variable from the DLL in the same way as you export functions. That way the DLL and the EXE both know about the exported global variable.

wildfrog
December 1st, 2006, 07:31 AM
Or what about passing the variables address to the DLL?

Something like:

// source of exe
int global_int;

//init dll
InitializeDll(&global_int); // now the DLL can store the variables address
// 'internally' and use it whenever it wants.

This way the DLL doesn't have to care about the 'global variable', where it's actually declared/defined and if its global or not.

- petter

stephenteh
December 1st, 2006, 08:51 AM
@ Zaccheus, wildfrog

Thanks for reply, actually I'm thinking the same way, export a function like SetVariable and let the exe to set the appriopriate variable for the dll to use.