在DLL访问一个全局变量 [英] Access a global variable in a dll

查看:675
本文介绍了在DLL访问一个全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问这是在我的应用程序的main()函数在DLL初始化的全局变量?

how do I access a global variable which is initialized in the main() function of my application in a dll?

我的全局变量包含了我需要一个dll锁定一个临界区对象。

My global variable contains a critical section object which I need to lock in a dll.

我试着将它导出我的应用程序,并使用它导入该dll

I've tried to export it in my application and import it in the dll using

__ declspec(dllexport)的 __ declspec(dllimport的)
但是当我试图锁定关键节,我收到了runtine例外,这让我觉得我的dll访问变量未正确初始化。

__declspec(dllexport) and __declspec(dllimport) but when I try to lock the critical section I get a runtine exception which makes me think that the variable my dll accesses is not initialized correctly.

该DLL在运行时使用调用LoadLibrary加载。

The dll is loaded at runtime using LoadLibrary.

任何提示将AP preciated。

Any hints would be appreciated.

推荐答案

一般来说,在一个DLL变量从应用程序访问,但它看起来像你想圆做它的其他方式。和你的应用程序,从出口和进口的DLL方式似乎有点hackish。

Generally variables in a DLL are accessed from the application, but it looks like you're trying to do it the other way round. And your way of exporting from application and importing in the DLL seems a bit hackish.

如何在创建您的DLL函数接受一个指向临界区对象从您的应用程序,并将其存储在DLL自己的全局变量?

How about creating a function in your DLL that takes a pointer to the critical section object from your application and stores it in the DLL's own global variable?

DLL:

CRITICAL_SECTION *gCS;

__declspec(dllexport) void MyDLL_SetCS(CRITICAL_SECTION *cs) {
  gCS = cs;
}

应用:

CRITICAL_SECTION cs;
// initialize cs here
MyDLL_SetCS(&cs);

然后DLL可以在需要时使用它自己的指针的副本。

Then the DLL can use its own copy of the pointer when required.

这篇关于在DLL访问一个全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆