今天工作闲下来, 就试看DLL编程, 好久没玩DLL了,结果花了一个半小时才写出这个很简单的例子.

起初直接把子__declspec(dllexport) 放在类声明的地方. 发生 warning C4273 DLL 链接不一致错误.

想了挺长时间才意识到是生成lib时并头文件和原文件中对函数的名字解析名字不一样而产生的.  才想到要定义一个宏.

有点感叹知识不用会也会老的. 下边是代码

//头文件:testClass.h

 

  1. #ifndef TEST_CLASS_H__   
  2. #define TEST_CLASS_H__   
  3.   
  4. #include <iostream>   
  5. using namespace std;  
  6.   
  7. #ifdef SERVERDLL_EXPORTS //在创建产生DLL的工程中先把 SERVERDLL_EXPORTS 预定义上   
  8. #define SERVERDLL_API __declspec(dllexport)   
  9. #else   
  10. #define SERVERDLL_API __declspec(dllimport)   
  11. #endif   
  12.   
  13. class SERVERDLL_API TestClass  
  14. {  
  15. public:  
  16.     virtual void VirtualFunction(void);  
  17.       
  18.     void NormalFunction(void);    
  19. };  
  20.   
  21. SERVERDLL_API void func(void);  
  22. #endif  

#ifndef TEST_CLASS_H__ #define TEST_CLASS_H__ #include <iostream> using namespace std; #ifdef SERVERDLL_EXPORTS //在创建产生DLL的工程中先把 SERVERDLL_EXPORTS 预定义上 #define SERVERDLL_API __declspec(dllexport) #else #define SERVERDLL_API __declspec(dllimport) #endif class SERVERDLL_API TestClass { public: virtual void VirtualFunction(void); void NormalFunction(void); }; SERVERDLL_API void func(void); #endif

//实现文件.testClass.cpp 

[c-sharp] view plain copy print ?
  1. #include "testClass.h"   
  2.   
  3.   
  4. void TestClass::VirtualFunction(void)  
  5.     {  
  6.         cout<< "this is VirtualFunction()!" <<endl;  
  7.         return;  
  8.     }  
  9. void TestClass::NormalFunction(void)  
  10.     {  
  11.         cout<< "this is NormalFunction()!" <<endl;  
  12.         return;  
  13.     }  
  14. void func(void)  
  15. {  
  16.     cout << "xixi haha" <<endl;  
  17.     return;  
  18. }  

#include "testClass.h" void TestClass::VirtualFunction(void) { cout<< "this is VirtualFunction()!" <<endl; return; } void TestClass::NormalFunction(void) { cout<< "this is NormalFunction()!" <<endl; return; } void func(void) { cout << "xixi haha" <<endl; return; }

//调试代码

  1. #include <iostream>   
  2. #include <windows.h>   
  3.   
  4. #include <e://myself//MyProject//网络程序//ServerDLL//testClass.h>    
  5.   
  6. using namespace std;  
  7. #pragma comment(lib, "E://myself//MyProject//网络程序//ServerDLL//Debug//ServerDLL.lib")   
  8.   
  9. //TestClass类为DLL导出的类   
  10. class MY : public TestClass  
  11. {  
  12. public:  
  13.         virtual void VirtualFunction()  
  14.     {  
  15.         cout<< "my VirtualFunction()" <<endl;  
  16.         return;  
  17.     }  
  18.     void NormalFunction()  
  19.     {  
  20.         cout<< "my NormalFunction()" <<endl;  
  21.         return;  
  22.     }  
  23. };  
  24. int main(void)  
  25. {  
  26.     MY my;  
  27.     TestClass *tc = &my;  
  28.   
  29.     tc->NormalFunction();  
  30.     tc->VirtualFunction();  
  31.     //调用导出的函数   
  32.     func();  
  33.   
  34.     cout<<"liu zhiliang"<<endl;  
  35.     system("PAUSE");  
  36.     return 0;  
  37. }  

#include <iostream> #include <windows.h> #include <e://myself//MyProject//网络程序//ServerDLL//testClass.h> using namespace std; #pragma comment(lib, "E://myself//MyProject//网络程序//ServerDLL//Debug//ServerDLL.lib") //TestClass类为DLL导出的类 class MY : public TestClass { public: virtual void VirtualFunction() { cout<< "my VirtualFunction()" <<endl; return; } void NormalFunction() { cout<< "my NormalFunction()" <<endl; return; } }; int main(void) { MY my; TestClass *tc = &my; tc->NormalFunction(); tc->VirtualFunction(); //调用导出的函数 func(); cout<<"liu zhiliang"<<endl; system("PAUSE"); return 0; }

输出:

  1. this is NormalFunction()!  
  2. my VirtualFunction()  
  3. xixi haha  
  4. liu zhiliang  
  5. 请按任意键继续. . .