dll的一个导出全局变量的,有关多个dll中共享一个变量的问题
我本来是打算使用A B C三个dll的,然后他们需要同时共享一个变量,达到每一个dll中同步的问题!
但是我目前是使用这样的一个情况,好像是不太正确,我对dll导出全局变量不太熟!望大家帮忙看看!下面是我的测试代码:
testA.h:
#ifndef __TESTA_H__
#define __TESTA_H__
/*extern __declspec(dllexport) int dllGlobalVar;*/
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif
extern MYLIBAPI int dllGlobalVar;
class MYLIBAPI TestA
{
public:
TestA();
~TestA();
void Show();
};
extern MYLIBAPI int *GetNumber();
#endif
#define MYLIBAPI __declspec(dllexport)
#include "testA.h"
#include "string"
#include "iostream"
using namespace std;
int dllGlobalVar;
TestA::TestA()
{
}
TestA::~TestA()
{
}
void TestA::Show()
{
char str[1024];
sprintf(str, "this is TestA the value is : %d", dllGlobalVar);
cout << "str value is :" << str << endl;
}
// 获取全局变量地址的函数
extern __declspec(dllexport) int *GetNumber()
{
return &dllGlobalVar;
}
#ifndef __TESTB_H__
#define __TESTB_H__
/*extern __declspec(dllexport) int dllGlobalVar;*/
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif
extern MYLIBAPI int dllGlobalVar;
class MYLIBAPI TestB
{
public:
TestB();
~TestB();
void Show();
};
extern MYLIBAPI int *GetNumber();
#endif
#define MYLIBAPI __declspec(dllexport)
#include "testB.h"
#include "string"
#include "iostream"
using namespace std;
int dllGlobalVar;
TestB::TestB()
{
}
TestB::~TestB()
{
}
void TestB::Show()
{
char str[1024];
sprintf(str, "this is TestB the value is : %d", dllGlobalVar);
cout << "str value is :" << str << endl;
}
// 获取全局变量地址的函数
extern __declspec(dllexport) int *GetNumber()
{
return &dllGlobalVar;
}
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "testA.h"
#include "testB.h"
#include "testC.h"
#pragma comment(lib, "testA.lib")
#pragma comment(lib, "testB.lib")
#pragma comment(lib, "testC.lib")
int _tmain(int argc, _TCHAR* argv[])
{
int *i = GetNumber();
*i = 7777;
TestA testa;
testa.Show();
TestB testb;
testb.Show();
return 0;
}
你这里只是对他get,就算你对每一个dll中的全局变量都有get set
这里可以get/set都可以了,不要另外的set。getA只需要在其中一个dll中导出。
不是太懂?要不然咱们各自写出自己的实现方式?
我觉得你的并不能确定每一个dll(和主程序中)都是使用的同一个全局变量
你试试哈。
原理来讲,每一个dll中dllGlobalVar都是不同的地址,也就是不同的值,get虽然肯定是不管在哪个dll。但是你设置一个dllGlobalVar,并不能同时修改每一个dllGlobalVar的值,就像lz目前的代码一样。testa.Show();和testb.Show();这两个并不是一样的值。
lz的问题,对你的方式做出来解答:输出的一个为7777另外一个为0
不知道是我理解不正确?还是有什么别的方式,可以避开“每一个dll中dllGlobalVar都是不同的地址”?
换句话, 这里没有共享。 main中getNumber只是选了其中一份而已。
#ifndef __TESTA_H__
#define __TESTA_H__
/*extern __declspec(dllexport) int dllGlobalVar;*/
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif
extern MYLIBAPI int* dllGlobalVar;
class MYLIBAPI TestA
{
public:
TestA();
~TestA();
void Show();
void SetNumber(int* value);
};
#endif
#define MYLIBAPI __declspec(dllexport)
#include "testA.h"
#include "string"
#include "iostream"
using namespace std;
int* dllGlobalVar;
TestA::TestA()
{
}
TestA::~TestA()
{
}
void TestA::Show()
{
char str[1024];
sprintf(str, "this is TestA the value is : %d", *dllGlobalVar);
cout << "str value is :" << str << endl;
cout << "this variable address is : " << dllGlobalVar << endl;
}
void TestA::SetNumber(int* value)
{
dllGlobalVar = value;
}
// 获取全局变量地址的函数
// extern __declspec(dllexport) int *GetNumber()
// {
// return &dllGlobalVar;
// }
#ifndef __TESTB_H__
#define __TESTB_H__
/*extern __declspec(dllexport) int dllGlobalVar;*/
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif
extern MYLIBAPI int* dllGlobalVar;
class MYLIBAPI TestB
{
public:
TestB();
~TestB();
void Show();
void SetNumber(int* value);
};
/*extern MYLIBAPI int *GetNumber();*/
#endif
#define MYLIBAPI __declspec(dllexport)
#include "testB.h"
#include "string"
#include "iostream"
using namespace std;
int* dllGlobalVar;
TestB::TestB()
{
}
TestB::~TestB()
{
}
void TestB::Show()
{
char str[1024];
sprintf(str, "this is TestB the value is : %d", *dllGlobalVar);
cout << "str value is :" << str << endl;
cout << "this variable address is : " << &dllGlobalVar << endl;
}
void TestB::SetNumber(int* value)
{
dllGlobalVar = value;
}
// 获取全局变量地址的函数
// extern __declspec(dllexport) int *GetNumber()
// {
// return &dllGlobalVar;
// }
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "testA.h"
#include "testB.h"
#pragma comment(lib, "testA.lib")
#pragma comment(lib, "testB.lib")
int _tmain(int argc, _TCHAR* argv[])
{
// int *i = GetNumber();
// *i = 7777;
int i = 99;
int* pI = &i;
TestA testa;
testa.SetNumber(pI);
testa.Show();
TestB testb;
testb.SetNumber(pI);
testb.Show();
i = 999;
testa.Show();
testb.Show();
i = 1;
testa.Show();
i = 2;
testb.Show();
return 0;
}