首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

dll的一个导出全局变量的,有关多个dll中共享一个变量的有关问题

2013-08-13 
dll的一个导出全局变量的,有关多个dll中共享一个变量的问题我本来是打算使用A B C三个dll的,然后他们需要

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


test.cpp:


#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;
}



testB.h:

#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



testB.cpp:


#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;
}




测试的exe:

// 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;
}



输出的一个为7777另外一个为0 全局变量 dll
[解决办法]
引用:
用全局函数代替全局变量,并导出这个全局函数,使用前确保dll还没在内存中

int& getA();


使用前确保dll还在内存中
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

你这里只是对他get,就算你对每一个dll中的全局变量都有get set

这里可以get/set都可以了,不要另外的set。getA只需要在其中一个dll中导出。


不是太懂?要不然咱们各自写出自己的实现方式?
我觉得你的并不能确定每一个dll(和主程序中)都是使用的同一个全局变量

你试试哈。 
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

你这里只是对他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只是选了其中一份而已。 

我写了一份(在lz的基础上),我自己觉得很一般,想看看你的:

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();

void SetNumber(int* value);
};


#endif



testA.cpp:


#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;
// }


testB.h:

#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


testB.cpp:


#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:

// 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;
}


花2 3分钟写一下,对你的这种方式很感兴趣!

热点排行