测试目的:测试static成员变量m_tblctrl,在不同库中的地址是否唯一性.
测试结果:导出类库中的静态成员地址,在不同的地方引用是唯一性.
准备工作,使用vc++6.0生成2个导出类的库和一个测试程序:
libTA
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the LIBTA_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// LIBTA_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef LIBTA_EXPORTS
#define LIBTA_API __declspec(dllexport)
#else
#define LIBTA_API __declspec(dllimport)
#endif
typedef struct tagTABLECTRL
{
int tblcount;
char tblcode[4][24];
} TABLECTRL, *LPTABLECTRL, *PTABLECTRL;
// This class is exported from the libTA.dll
class LIBTA_API CLibTA
{
public:
CLibTA(void);
// TODO: add your methods here.
public:
void testA(int nTblNo);
void printfTbl();
protected:
static TABLECTRL m_tblctrl;
};
extern LIBTA_API int nLibTA;
LIBTA_API int fnLibTA(void);
// libTA.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "libTA.h"
TABLECTRL CLibTA::m_tblctrl;
// This is an example of an exported variable
LIBTA_API int nLibTA=0;
// This is an example of an exported function.
LIBTA_API int fnLibTA(void)
{
CLibTA a;
a.testA(0);
return 'A';
}
// This is the constructor of a class that has been exported.
// see libTA.h for the class definition
CLibTA::CLibTA()
{
return;
}
void CLibTA::testA(int nTblNo)
{
if (0 == nTblNo)
{
m_tblctrl.tblcount = 1;
strcpy(m_tblctrl.tblcode[0], "myself");
}
else if (1 == nTblNo)
{
m_tblctrl.tblcount = 2;
strcpy(m_tblctrl.tblcode[1], "libTA");
}
else if (2 == nTblNo)
{
m_tblctrl.tblcount = 3;
strcpy(m_tblctrl.tblcode[2], "libTB");
}
else if (3 == nTblNo)
{
m_tblctrl.tblcount = 4;
strcpy(m_tblctrl.tblcode[3], "testStatic");
}
}
void CLibTA::printfTbl()
{
printf("tblcount:%d\n", m_tblctrl.tblcount);
for (int i = 0; i < 4; i++)
{
printf("tblcode:%s\n", m_tblctrl.tblcode[i]);
}
}
libTB
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the LIBTA_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// LIBTA_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef LIBTB_EXPORTS
#define LIBTB_API __declspec(dllexport)
#else
#define LIBTB_API __declspec(dllimport)
#endif
// This class is exported from the libTA.dll
class LIBTB_API CLibTB
{
public:
CLibTB(void);
// TODO: add your methods here.
public:
void testB(int nTblNo);
protected:
};
extern LIBTB_API int nLibTB;
LIBTB_API int fnLibTB(void);
// libTB.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "libTB.h"
#include "../libTA/libTA.h"
// This is an example of an exported variable
LIBTB_API int nLibTB=0;
// This is an example of an exported function.
LIBTB_API int fnLibTB(void)
{
return 'B';
}
// This is the constructor of a class that has been exported.
// see libTA.h for the class definition
CLibTB::CLibTB()
{
return;
}
void CLibTB::testB(int nTblNo)
{
CLibTA b;
b.testA(nTblNo);
}
testStatic
// testStatic.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "../libTA/libTA.h"
#include "../libTB/libTB.h"
// 测试目的:测试static成员变量m_tblctrl,在不同库中的地址是否唯一性.
// 测试结果:导出类库中的静态成员地址,在不同的地方引用是唯一性.
int main(int argc, char* argv[])
{
fnLibTA();
CLibTA a;
a.testA(1);
CLibTB b;
b.testB(2);
CLibTA c;
c.testA(3);
CLibTA d;
d.printfTbl();
return 0;
}
结果
tblcount:4
tblcode:myself
tblcode:libTA
tblcode:libTB
tblcode:testStatic