1、VS2003新建DLL项目dllTest
2、项目dllTest中添加脚本point.h,代码如下:
1 #ifndef POINT_H 2 #define POINT_H 3 4 #ifdef DLL_FILE 5 class _declspec(dllexport) point //导出类point 6 #else 7 class _declspec(dllimport) point //导入类point 8 #endif 9 { 10 public: 11 float y; 12 float x; 13 point(); 14 point(float x_coordinate,float y_coordinate ); 15 }; 16 17 #endif
3、项目dllTest中添加脚本circle.h,代码如下:
1 #ifndef CIRCLE_H 2 #define CIRCLE_H 3 4 #include "point.h" 5 #ifdef DLL_FILE 6 class _declspec(dllexport) circle //导出类circle 7 #else 8 class _declspec(dllimport) circle //导入类circle 9 #endif 10 { 11 public: 12 void SetCentre(const point ¢rePoint); 13 void SetRadius(float r); 14 float GetGirth(); 15 float GetArea(); 16 circle(); 17 private: 18 float radius; 19 point centre; 20 }; 21 22 #endif
4、项目dllTest中添加脚本point.cpp,代码如下:
1 #ifndef DLL_FILE 2 #define DLL_FILE 3 #endif 4 5 #include "point.h" 6 7 ////////////////////////////////////////////////////////////////////// 8 // Construction/Destruction 9 ////////////////////////////////////////////////////////////////////// 10 11 point::point() 12 { 13 } 14 point::point(float x_coordinate,float y_coordinate) 15 { 16 x = x_coordinate; 17 y = y_coordinate; 18 }
5、项目dllTest中添加脚本circle.cpp,代码如下:
1 #ifndef DLL_FILE 2 #define DLL_FILE 3 #endif 4 5 #include "circle.h" 6 #define PI 3.1415926 7 circle::circle () 8 { 9 centre = point(0,0); 10 radius = 0; 11 } 12 float circle::GetArea() 13 { 14 return PI*radius*radius; 15 } 16 float circle::GetGirth() 17 { 18 return 2*PI*radius; 19 } 20 void circle::SetCentre(const point ¢rePoint) 21 { 22 centre = centrePoint; 23 } 24 void circle::SetRadius(float r) 25 { 26 radius = r; 27 }
6、build生成dllTest.dll文件
7、添加检测项目dllCall
8、添加主程序脚本dllCall.cpp,代码如下:
1 #include "stdafx.h" 2 #include <windows.h> 3 #include "..circle.h" 4 5 #pragma comment(lib,"..\Debug\dllTest.lib"); 6 7 int main(int argc, char* argv[]) 8 { 9 circle c; 10 point p(2.0,2.0); 11 c.SetCentre(p); 12 c.SetRadius(1.0); 13 printf("area:%f girth:%f ",c.GetArea(),c.GetGirth()); 14 15 return 0; 16 }
9、Ctrl+F5调试运行结果如下: