C++ DLL 模板 .

 C++ DLL 模板

1、使用VS2005创建Win32 DLL项目,选择空项目,然后加入CppDll.h和CppDll.cpp文件。

2、修改CppDll.h和CppDll.cpp文件使之成为需要的内容。

3、编译生成CppDll.dll。

下面是模板文件:

  1. //   
  2. // CppDll.h   
  3. // by cheungmine   
  4. // C++ DLL 模板   
  5. //   
  6. /*** 使用CPPDLL: 
  7. #include "../CppDll.h" 
  8. #ifdef _DEBUG 
  9. #  pragma comment(lib, "F:/del/CppDll/Debug/CppDlld.lib") 
  10. #else 
  11. #  pragma comment(lib, "F:/del/CppDll/Release/CppDll.lib") 
  12. #endif 
  13. ***/  
  14. #ifndef _CPPDLL_H__   
  15. #define _CPPDLL_H__   
  16. //#include <windows.h>   
  17. //#include <math.h>   
  18. //#include <assert.h>   
  19. //#include <memory.h>   
  20. //#include <malloc.h>   
  21. // 下列 ifdef 块是创建使从 DLL 导出更简单的宏的标准方法。   
  22. // 此 DLL 中的所有文件都是用命令行上定义的 CPPDLL_EXPORTS 符号编译的。   
  23. // 在使用此 DLL 的任何其他项目上不应定义此符号。   
  24. // 这样,源文件中包含此文件的任何其他项目都会将 CPPDLL_API 函数视为是从此 DLL 导入的,   
  25. // 而此 DLL 则将用此宏定义的符号视为是被导出的。   
  26. #ifdef CPPDLL_EXPORTS   
  27. #define CPPDLL_API __declspec(dllexport)   
  28. #else   
  29. #define CPPDLL_API __declspec(dllimport)   
  30. #endif   
  31. #define     CPPDLL_VERSION   1.0            // 常量定义   
  32. // 名称空间   
  33. namespace CppDll  
  34. {  
  35. //   
  36. // 从 CppDll.dll 导出类   
  37. //   
  38. // 导出类: MyStruct   
  39. struct CPPDLL_API MyStruct  
  40. {  
  41.     long    x;  
  42.     long    y;  
  43. };  
  44. // 导出类: MyClass2   
  45. class CPPDLL_API MyClass2  
  46. {  
  47.     void Clear()  
  48.     {  
  49.         // 实现   
  50.     };  
  51. public:  
  52.     MyClass2();  
  53.     ~MyClass2();  
  54. };  
  55. // 导出共享变量   
  56. extern CPPDLL_API int g_nVar;  
  57. //   
  58. // 导出方法   
  59. //   
  60. CPPDLL_API double Method(const MyStruct *s1, const MyStruct *s2);  
  61. CPPDLL_API double Method(const MyStruct &s1, const MyStruct &s2);  
  62.   
  63. };   // End of namespace CppDll   
  64. #endif  // _CPPDLL_H__  

 

  1. //   
  2. // CppDll.cpp   
  3. // by cheungmine   
  4. //   
  5. #include "CppDll.h"   
  6. // 包含其他必要文件   
  7. // #include <vector>   
  8. using namespace CppDll;  
  9. ///////////////////////////////////////////////////////////////////////////////   
  10. // struct MyStruct   
  11.   
  12.   
  13. ///////////////////////////////////////////////////////////////////////////////   
  14. // class MyClass2   
  15. MyClass2::MyClass2()  
  16. {  
  17. }  
  18.   
  19. MyClass2::~MyClass2()  
  20. {  
  21. }  
  22. ///////////////////////////////////////////////////////////////////////////////   
  23. // 导出变量   
  24. CPPDLL_API int g_nVar = 0;  
  25. ///////////////////////////////////////////////////////////////////////////////   
  26. // 导出方法   
  27. CPPDLL_API double CppDll::Method(const MyStruct *s1, const MyStruct *s2)  
  28. {  
  29.     return 0;  
  30. }  
  31. CPPDLL_API double CppDll::Method(const MyStruct &s1, const MyStruct &s2)  
  32. {  
  33.     return 0;  
  34. }  
原文地址:https://www.cnblogs.com/lidabo/p/3483123.html