C++ DLL 模板

 C++ DLL 模板

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

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

3、编译生成CppDll.dll。

下面是模板文件:

// // CppDll.h // by cheungmine // C++ DLL 模板 // /*** 使用CPPDLL: #include "../CppDll.h" #ifdef _DEBUG # pragma comment(lib, "F:/del/CppDll/Debug/CppDlld.lib") #else # pragma comment(lib, "F:/del/CppDll/Release/CppDll.lib") #endif ***/ #ifndef _CPPDLL_H__ #define _CPPDLL_H__ //#include <windows.h> //#include <math.h> //#include <assert.h> //#include <memory.h> //#include <malloc.h> // 下列 ifdef 块是创建使从 DLL 导出更简单的宏的标准方法。 // 此 DLL 中的所有文件都是用命令行上定义的 CPPDLL_EXPORTS 符号编译的。 // 在使用此 DLL 的任何其他项目上不应定义此符号。 // 这样,源文件中包含此文件的任何其他项目都会将 CPPDLL_API 函数视为是从此 DLL 导入的, // 而此 DLL 则将用此宏定义的符号视为是被导出的。 #ifdef CPPDLL_EXPORTS #define CPPDLL_API __declspec(dllexport) #else #define CPPDLL_API __declspec(dllimport) #endif #define CPPDLL_VERSION 1.0 // 常量定义 // 名称空间 namespace CppDll { // // 从 CppDll.dll 导出类 // // 导出类: MyStruct struct CPPDLL_API MyStruct { long x; long y; }; // 导出类: MyClass2 class CPPDLL_API MyClass2 { void Clear() { // 实现 }; public: MyClass2(); ~MyClass2(); }; // 导出共享变量 extern CPPDLL_API int g_nVar; // // 导出方法 // CPPDLL_API double Method(const MyStruct *s1, const MyStruct *s2); CPPDLL_API double Method(const MyStruct &s1, const MyStruct &s2); }; // End of namespace CppDll #endif // _CPPDLL_H__

 

// // CppDll.cpp // by cheungmine // #include "CppDll.h" // 包含其他必要文件 // #include <vector> using namespace CppDll; /////////////////////////////////////////////////////////////////////////////// // struct MyStruct /////////////////////////////////////////////////////////////////////////////// // class MyClass2 MyClass2::MyClass2() { } MyClass2::~MyClass2() { } /////////////////////////////////////////////////////////////////////////////// // 导出变量 CPPDLL_API int g_nVar = 0; /////////////////////////////////////////////////////////////////////////////// // 导出方法 CPPDLL_API double CppDll::Method(const MyStruct *s1, const MyStruct *s2) { return 0; } CPPDLL_API double CppDll::Method(const MyStruct &s1, const MyStruct &s2) { return 0; }

你可能感兴趣的:(C++,c,struct,api,Class,dll)