Objective: To test whether the static member variable m_tblctrl is unique in different libraries.
Test result: the static member address in the exported class library is unique in different places.
Preparations: Use VC ++ 6.0 to generate two export class libraries and a test program:
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)#endiftypedef struct tagTABLECTRL{int tblcount;char tblcode[4][24];} TABLECTRL, *LPTABLECTRL, *PTABLECTRL;// This class is exported from the libTA.dllclass 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 variableLIBTA_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 definitionCLibTA::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.dllclass 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 variableLIBTB_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 definitionCLibTB::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. H "# include ".. /libtb. H "// purpose: to test whether the static member variable m_tblctrl is unique in different libraries. // test result: the static member address in the exported class library is unique in different places. int main (INT argc, char * argv []) {fnlibta (); clibta A;. testa (1); clibtb B; B. testb (2); clibta C; C. testa (3); clibta D; D. printftbl (); Return 0 ;}
Result
Tblcount: 4
Tblcode: myself
Tblcode: libta
Tblcode: libtb
Tblcode: teststatic