1. 程式人生 > >C++封裝DLL,匯出一個類

C++封裝DLL,匯出一個類



首先:建一個DLL工程,win32專案--DLL--確定;

在標頭檔案中定義要匯出的類:

  1. class  _declspec(dllexport) CWordSegment  
  2. {  
  3. public:  
  4.     CWordSegment();  
  5.     ~CWordSegment();  
  6.     bool CWordSegmentInit();  
  7.     char *CWordSegmentResolve();  
  8. };

在相應的.cpp檔案中實現:

  1. #include "CWordSegment.h"
  2. #include 
  3.  CWordSegment::CWordSegment()  
  4. {  
  5.     printf("CWordSegment constructed /n");  
  6. }  
  7. CWordSegment::~CWordSegment()  
  8. {  
  9.     printf("CWordSegment disconstructed /n");  
  10. }  
  11.  bool CWordSegment::CWordSegmentInit()  
  12. {  
  13.     printf("CWordSegmentInit /n");  
  14.     returntrue;  
  15. }  
  16. char* CWordSegment::CWordSegmentResolve()  
  17. {  
  18.     printf("CWordSegmentResolve /n"
    );  
  19.     return NULL;  
  20. }

三、編譯原始檔(快捷鍵F7),在../CWordSegment/Debug目錄下生成DLL的兩個最終要的檔案,這個應該不用解釋了吧

CWordSegment.dll

CWordSegment.lib 

  就此,DLL就搞好了,

下面如何呼叫這個lib檔案:

也很簡單:建一個測試工程控制檯應用程式:

然後把上面生產的庫檔案加進來,可以用相對路徑也可以用絕對路徑;這個隨便;

一般相對路徑加入的方法:

(1)連結器-->常規-->附加依賴目錄 加入進來即可;

(2)連結器-->常規-->附加依賴項 加入lib庫名稱;

然後就是匯出類:

  1. 原始碼檔案:testDll.h 
  2. 此處宣告匯入類,把第一節CWordSegment.h檔案中的類定義COPY過來, 
  3. 然後把匯出改為匯入,即更改巨集:  
  4. 由_declspec(dllexport) 匯出巨集--->> 改為 
  5.           --->>>  _declspec(dllimport) 倒入巨集 
  6. 很簡單吧 :) 
  7. */
  8. class  _declspec(dllimport) CWordSegment  
  9. {  
  10. public:  
  11.     CWordSegment();  
  12.     ~CWordSegment();  
  13.     bool CWordSegmentInit();  
  14.     char *CWordSegmentResolve();  
  15. }; 

測試程式如下:

  1. // testDll.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "testDll.h" 
  5. CWordSegment test;  
  6. int main(int argc, char* argv[])  
  7. {  
  8.     test.CWordSegmentInit();  
  9.     printf("Hello World!/n");  
  10.     return 0;  

首先:建一個DLL工程,win32專案--DLL--確定;

在標頭檔案中定義要匯出的類:

  1. class  _declspec(dllexport) CWordSegment  
  2. {  
  3. public:  
  4.     CWordSegment();  
  5.     ~CWordSegment();  
  6.     bool CWordSegmentInit();  
  7.     char *CWordSegmentResolve();  
  8. };

在相應的.cpp檔案中實現:

  1. #include "CWordSegment.h"
  2. #include 
  3.  CWordSegment::CWordSegment()  
  4. {  
  5.     printf("CWordSegment constructed /n");  
  6. }  
  7. CWordSegment::~CWordSegment()  
  8. {  
  9.     printf("CWordSegment disconstructed /n");  
  10. }  
  11.  bool CWordSegment::CWordSegmentInit()  
  12. {  
  13.     printf("CWordSegmentInit /n");  
  14.     returntrue;  
  15. }  
  16. char* CWordSegment::CWordSegmentResolve()  
  17. {  
  18.     printf("CWordSegmentResolve /n");  
  19.     return NULL;  
  20. }

三、編譯原始檔(快捷鍵F7),在../CWordSegment/Debug目錄下生成DLL的兩個最終要的檔案,這個應該不用解釋了吧

CWordSegment.dll

CWordSegment.lib 

  就此,DLL就搞好了,

下面如何呼叫這個lib檔案:

也很簡單:建一個測試工程控制檯應用程式:

然後把上面生產的庫檔案加進來,可以用相對路徑也可以用絕對路徑;這個隨便;

一般相對路徑加入的方法:

(1)連結器-->常規-->附加依賴目錄 加入進來即可;

(2)連結器-->常規-->附加依賴項 加入lib庫名稱;

然後就是匯出類:

  1. 原始碼檔案:testDll.h 
  2. 此處宣告匯入類,把第一節CWordSegment.h檔案中的類定義COPY過來, 
  3. 然後把匯出改為匯入,即更改巨集:  
  4. 由_declspec(dllexport) 匯出巨集--->> 改為 
  5.           --->>>  _declspec(dllimport) 倒入巨集 
  6. 很簡單吧 :) 
  7. */
  8. class  _declspec(dllimport) CWordSegment  
  9. {  
  10. public:  
  11.     CWordSegment();  
  12.     ~CWordSegment();  
  13.     bool CWordSegmentInit();  
  14.     char *CWordSegmentResolve();  
  15. }; 

測試程式如下:

  1. // testDll.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "testDll.h" 
  5. CWordSegment test;  
  6. int main(int argc, char* argv[])  
  7. {  
  8.     test.CWordSegmentInit();  
  9.     printf("Hello World!/n");  
  10.     return 0;