2017农村宅基地贷款:求教全局变量的使用问题

来源:百度文库 编辑:中科新闻网 时间:2024/05/01 01:56:27
定义Dll接口函数SetTrack,GetTrack 与全局变量myInt
通过外部程序可以设置和获取myInt的值,但在Dll内部的类中引用
myInt的值又不是上面所设置的值,有什么方法解决么?
// stdafx.cpp : source file that includes just the standard includes
// stdafx.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
//int myInt;
#include "stdafx.h"

#ifdef _ATL_STATIC_REGISTRY
#include <statreg.cpp>
#endif

#include <atlimpl.cpp>

int myInt;

void __stdcall SetTrack(int s)
{
myInt=s;
}

int __stdcall GetTrack(int s)
{
return myInt;
}

把你的全局变量添加到CXXXApp类中,XXX是你的工程名字;
然后在CXXXApp类定义结束的地方加一句:

extern CXXXApp theApp;

然后,你就可以在你的程序中的任何地方用如下的形式来访问你的全局变量了:

theApp.???

???就是你在CXXXApp中添加的变量,也就是你的变量了。。

这样一来,就把所有的全局变量用CXXXApp进行了封装。

这个问题是这样的:你的每一个DLL的使用者都会由操作系统提供一个堆叠,所以你的那个虽是一个全局变量,但是它会放在由不同使用者所生的堆叠中。解决的方法是把DLL的变量声明在一个地址空间,这个在DLL编程中用的是一个#pragma,预编译指令定义的。具体的你可以查一下programing windows 95这本书。里面有例子。