返回> 网站首页 

[转载]Dll 导出类 [示例代码]

yoours2012-02-18 12:23:39 阅读 1383

简介一边听听音乐,一边写写文章。

1、Dll相关代码

 

MyDll.h

  1. #ifdef DLL1_API  
  2. #else  
  3. #define DLL1_API extern "C" __declspec(dllimport)  
  4. #endif  
  5. DLL1_API int Add(int a,int b);  
  6. DLL1_API int Sub(int a,int b);  
  7. class __declspec(dllexport) Person  
  8. {  
  9. public:  
  10.     Person(char *name);  
  11.     char*   m_Name;  
  12.     int     m_Age;  
  13. };  
  1. #ifdef DLL1_API  
  2. #else  
  3. #define DLL1_API extern "C" __declspec(dllimport)  
  4. #endif  
  5. DLL1_API int Add(int a,int b);  
  6. DLL1_API int Sub(int a,int b);  
  7. class __declspec(dllexport) Person  
  8. {  
  9. public:  
  10.     Person(char *name);  
  11.     char*   m_Name;  
  12.     int     m_Age;  
  13. };  

 

MyDll.cpp

  1. #define DLL1_API extern "C" __declspec(dllexport)  
  2. #include "MyDll.h"  
  3. #include <Windows.h>  
  4. #include <stdio.h>  
  5. #pragma comment(linker,"/DLL")  
  6. #pragma comment(linker,"/ENTRY:DllMain")  
  7. int Add(int a,int b)  
  8. {  
  9.     return a+b;  
  10. }  
  11. int Sub(int a,int b)  
  12. {  
  13.     return a-b;  
  14. }  
  15. Person::Person(char *name)  
  16. {  
  17.     m_Name = name;  
  18. }  
  1. #define DLL1_API extern "C" __declspec(dllexport)  
  2. #include "MyDll.h"  
  3. #include <Windows.h>  
  4. #include <stdio.h>  
  5. #pragma comment(linker,"/DLL")  
  6. #pragma comment(linker,"/ENTRY:DllMain")  
  7. int Add(int a,int b)  
  8. {  
  9.     return a+b;  
  10. }  
  11. int Sub(int a,int b)  
  12. {  
  13.     return a-b;  
  14. }  
  15. Person::Person(char *name)  
  16. {  
  17.     m_Name = name;  
  18. }  

 

2、调用dll中类

 

Main.cpp

  1. #include <iostream.h>  
  2. #include <stdio.h>  
  3. #include <windows.h>  
  4. #include "MyDll.h"  
  5. #pragma comment(lib,"MyDll.lib")  
  6. void main()  
  7. {  
  8.     int x=3;  
  9.     int y=9;  
  10.     int z=Add(x,y);  
  11.     printf("%d+%d=%d /r/n", x,y,z);  
  12.       
  13.     Person pt("123");  
  14.     cout<<pt.m_Name<<endl;  
  15. }  
  1. #include <iostream.h>  
  2. #include <stdio.h>  
  3. #include <windows.h>  
  4. #include "MyDll.h"  
  5. #pragma comment(lib,"MyDll.lib")  
  6. void main()  
  7. {  
  8.     int x=3;  
  9.     int y=9;  
  10.     int z=Add(x,y);  
  11.     printf("%d+%d=%d /r/n", x,y,z);  
  12.       
  13.     Person pt("123");  
  14.     cout<<pt.m_Name<<endl;  
  15. }  

 


微信小程序扫码登陆

文章评论

1383人参与,0条评论