vc++ dll 中导出/导入全局变量的情况出现问题
前提
环境: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自我解决了我的问题。
这是因为在我的循环依赖删除工作之后 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.