vc++ dll 中导出/导入全局变量的情况出现问题

发布于 2024-10-06 01:10:45 字数 1046 浏览 0 评论 0原文

前提

环境:VC2005

我在项目中发现循环依赖(两个dll相互引用)时遇到了这个问题,因此将一个dll分成两个dll。

用示例解释:

typedef struct { <br>
    char myFileName[MAX_PATH];
} MyStructure;

之前:

MyHeader.h

MyStructure globalStruct;

之后:

MyCommon.h

#ifdef _MYGLOBAL_
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif

API extern MyStructure globalStruct;

MyGlobal.c

API MyStructure globalStruct;

MySpecific.c

API MyStructure globalStruct;

更改后失败代码:(此代码在特定dll中运行)

handle = LoadLibrary(globalStruct.myFileName);

我确认globalStruct.myFileName值是相同的(至少是真实的单词)。

更改后我无法直接使用调试器访问该值。因此,为了查看调试器中的值,我复制到临时本地 char[] 变量。然后,我发现尾部虚拟字符的前值和后值不同。

之前:
d:[我的路径] '0'(char end null) '0' '0' ... 之后:
d:[我的路径] '0'(char end null) '-3' '-3' '-3' ...

感谢您的阅读。

Precondition

environment : VC2005

I encountered this problem when discovered circular dependency in my project(two dll references each other), so divide either of one dll to two dll's.

explain with example :

typedef struct { <br>
    char myFileName[MAX_PATH];
} MyStructure;

Before :

MyHeader.h

MyStructure globalStruct;

After:

MyCommon.h

#ifdef _MYGLOBAL_
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif

API extern MyStructure globalStruct;

MyGlobal.c

API MyStructure globalStruct;

MySpecific.c

API MyStructure globalStruct;

failed code after changed: (this code run in specific dll)

handle = LoadLibrary(globalStruct.myFileName);

I confirmed globalStruct.myFileName values are same(at lease real words).

I can't access the value directly with debugger after changed. so, to see value in debugger, I copyed to temp local char[] variable. And then, I found before value and after value are different in tailing dummy char's.

before:
d:[my path] '0'(char end null) '0' '0' ...
after:
d:[my path] '0'(char end null) '-3' '-3' '-3' ...

thank you for your reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网名女生简单气质 2024-10-13 01:10:45

自我解决了我的问题。

这是因为在我的循环依赖删除工作之后 dll 没有重新编译。

所以,这个问题与导入/导出全局变量无关,而是dll重新编译问题。

抱歉我的问题很丑。

self-solved my problem.

This is because dll does not re-compiled after my circular dependency removal work.

So, this problem is not related import/export global variable but dll re-compile issue.

Sorry for my ugly question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文