一个全局变量能在定义它的模块之外的其他模块(运行时所有模块在同一进程中)中的函数访问吗?解决方法
一个全局变量能在定义它的模块之外的其他模块(运行时所有模块在同一进程中)中的函数访问吗?
如题,假设int g_nCount;定义在module1.exe模块中,module1.exe加载了module2.dll库模块和module3.dll库模块,如何在module2.dll和module3.dll库模块中引用module1.exe模块中定义的全局变量g_nCount?
我知道:在模块内的static全局变量可以被模块内所用函数访问,但不能被模块外其它函数访问;所以用static int g_nCount;肯定是不行的,多谢高手指点.着急,欢迎大家帮忙顶!顶者有分,分不够可以再多给点.
------解决方案--------------------
写个函数返回呗
------解决方案--------------------
那就再其他模块里也定义全局变量,通过接口函数的参数来传递全局变量。
------解决方案--------------------
static int g_nCount;是可以的
在exe程序中declare static int g_nCount;然后在dll中declare extern static int g_nCount
------解决方案--------------------
楼上的方法可行.
------解决方案--------------------
in the DLL
declare an interface pointer
in the exe
assign an object that implements the interface to the interface pointer
------解决方案--------------------
in your DLL
IConfig * g_config;
In your Exe
g_config=new CConfig();
struct IConfig
{
virtual int GetCount()=0;
}
------解决方案--------------------
定义abstract class,各个dll实现virtual interface
如题,假设int g_nCount;定义在module1.exe模块中,module1.exe加载了module2.dll库模块和module3.dll库模块,如何在module2.dll和module3.dll库模块中引用module1.exe模块中定义的全局变量g_nCount?
我知道:在模块内的static全局变量可以被模块内所用函数访问,但不能被模块外其它函数访问;所以用static int g_nCount;肯定是不行的,多谢高手指点.着急,欢迎大家帮忙顶!顶者有分,分不够可以再多给点.
------解决方案--------------------
写个函数返回呗
------解决方案--------------------
那就再其他模块里也定义全局变量,通过接口函数的参数来传递全局变量。
------解决方案--------------------
static int g_nCount;是可以的
在exe程序中declare static int g_nCount;然后在dll中declare extern static int g_nCount
------解决方案--------------------
楼上的方法可行.
------解决方案--------------------
in the DLL
declare an interface pointer
in the exe
assign an object that implements the interface to the interface pointer
------解决方案--------------------
in your DLL
IConfig * g_config;
In your Exe
g_config=new CConfig();
struct IConfig
{
virtual int GetCount()=0;
}
------解决方案--------------------
定义abstract class,各个dll实现virtual interface