1.創建dll工程

          以創建win32 dll程序為例,一般有兩種方式:

          一種是建立lib鏈接方式的dll(靜態鏈接,使用的時候需要lib)

          #ifdef __cplusplus 

          #define EXPORT extern "C"__declspec(dllexport)

          #else

          #define EXPORT __declspec(dllexport)

          #endif

          EXPORT int HelloWorld()

          {

          cout << "hello world" << endl;

          return 0;

          }

          第二種是為工程創建def文件,生成不需要libdll文件:

          如下:(先生成一個def文件)

          LIBRARY "computer"

          EXPORTS

          add PRIVATE

          而在代碼里只需要用:

          DllMain 前面加上你自己要導出的函數:

          int add(int x,int y)

          return(x + y);

          }

          而在使用的時候:

          HMODULE hDll = ::LoadLibrary(TEXT("computer.dll"));

          //typedef int pHelloWorld();

          //pHelloWorld *pHello = (pHelloWorld *)::GetProcAddress(hDll, "HelloWorld");

          typedef int (*pHelloWorld)();

          pHelloWorld pHello = (pHelloWorld)::GetProcAddress(hDll, "HelloWorld");

          int a = pHello();

          2.上面是最簡單的方式,弊端別人可以輕易的使用我們的dll。

          如果我們要想著封裝下免得被其他人隨意使用,于是就有了導出函數指針,創建對象的思路了...具體闡述如下:

          創建一個接口文件,供使用者參考。dll里面提供導出函數指針,創建這個接口的現實類對象,利用這個對象就可以使用其中的功能了。

          a ) 創建一個publish文件(提供給使用者)

          比如: computer_def.h

          class Icomputer
          {
          public:
              
          virtual int add(int a, int b ) = 0;
              
          virtual void del() = 0;
          };

          當然不要忘記書寫你的def文件:

          LIBRARY    "computer"
          EXPORTS
          DllGetClassObject    PRIVATE


          在dll中:

          class Ccomputer : public Icomputer
          {
          public:
              
          virtual int add(int a , int b)
              {
                  
          return a + b;
              }
              
          virtual void del()
              {

                  delete 
          this;
              }

          };

          HRESULT __stdcall DllGetClassObject(Icomputer
          ** ppv)
          {
              
          if( ppv == NULL )
                  
          return E_INVALIDARG;

              
          *ppv = (Icomputer*)(new Ccomputer());

              
          if*ppv == NULL )
                  
          return E_OUTOFMEMORY;

               
          return S_OK;

          }


          完成接口實現。提供導出函數。

          在使用的工程中,記得引入頭文件 computer_def.h文件,然后:

          Icomputer *pComputer;

                  HMODULE hDll 
          = ::LoadLibrary(TEXT("computer.dll"));
                  
                  typedef HRESULT (__stdcall 
          *PFN_DllGetClassObject)(Icomputer** ppv);

                  PFN_DllGetClassObject pDllGetClassObject 
          = (PFN_DllGetClassObject)::GetProcAddress(hDll, "DllGetClassObject");
                  
                  
          if(NULL == pDllGetClassObject)
                  {
                      
          //nRet = STATUS_SEVERITY_ERROR;
                  }

                  
          // 創建接口
                  HRESULT hRet = pDllGetClassObject(&pComputer);

          使用的時候:

          int iRet = pComputer->add(iNum_1,iNum_2);

             pComputer->del();


          記得在使用完畢時,FreeLibrary(hDll); 釋放資源。
          posted on 2009-08-17 23:04 -274°C 閱讀(1054) 評論(0)  編輯  收藏 所屬分類: C++

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 917238
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 阜新| 龙海市| 株洲市| 普定县| 西峡县| 台中市| 信阳市| 唐河县| 芒康县| 久治县| 临西县| 四子王旗| 长丰县| 宁都县| 淅川县| 滁州市| 龙南县| 阿鲁科尔沁旗| 青川县| 天台县| 太谷县| 公主岭市| 游戏| 当阳市| 鄂托克前旗| 临猗县| 海南省| 崇仁县| 达日县| 福清市| 大竹县| 鞍山市| 蕲春县| 镇雄县| 车致| 清苑县| 勃利县| 汤原县| 邵武市| 郎溪县| 无锡市|