全局外部指针变量在 DLL 中不可见

Global extern pointer variable not visible in DLL

本文关键字:DLL 外部 指针 变量 全局      更新时间:2023-10-16

>我有一个主应用程序EXE "main.EXE"。我在头文件 globalf.h 中创建了一个全局指针,其中包含 extern,如下所示:

全球F.H

extern SomeClass* cPtr;

现在在主.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 代码。

我大体上知道这个宣言。EXE 的 Initialize(( 对 不可见 DLL。但是我该如何实现这一点呢?有没有办法C++处理 跨 DLL 和 EXE 的此类全局变量。

我不是导出全局指针变量,而是将其以及您需要在 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);