2017-08-03 62 views
0

我有一个主应用程序EXE“main.EXE”。我创建了一个头文件globalf.h全局指针与EXTERN一样:全局extern指针变量在DLL中不可见

globalf.h现在

extern SomeClass* cPtr; 

在MAIN.EXE代码

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

D. DLL:

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

我无法访问DLL代码中的全局指针变量,即使我已声明inMain.EXE代码,即使是 也是如此。

我知道main.EXE的Initialize()中的这个声明对于 DLL是不可见的。但是,我怎么做到这一点?有没有办法在C++中处理 这样的全局变量DLL & EXE。

+1

你或许应该读作[恰好全局和静态变量中,当它被动态链接共享库什么?](https://stackoverflow.com/questions/19373061/what-happens-to-global-和静态变量-IN-A-共享库时-IT-是-DYNAM)。也许[共享进程和DLL之间的全局/静态变量](https://stackoverflow.com/questions/4911994/sharing-a-global-static-variable-between-a-process-and-dll)。或者[在dll和exe中使用全局变量](https://stackoverflow.com/questions/14197237/using-global-variable-in-dll-and-exe)。 –

+0

或者只是搜索['exe dll shared global variable'](https://www.google.se/search?q=exe+dll+shared+global+variable)提供的任何链接。 –

回答

0

而不是导出一个全局指针变量,我会打包它和其他变量,你需要在EXE和结构中的DLL之间共享,然后传递一个指向该结构的指针作为函数参数,例如到一个DLL初始化函数。

// 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);