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 閱讀(1049) 評論(0)  編輯  收藏 所屬分類: C++

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916110
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 文化| 汉阴县| 莒南县| 楚雄市| 克什克腾旗| 泰安市| 锡林浩特市| 罗甸县| 洛阳市| 康平县| 开封县| 五华县| 新建县| 无棣县| 梓潼县| 泸溪县| 莎车县| 镇安县| 崇礼县| 体育| 普洱| 丹凤县| 江油市| 封开县| 铅山县| 唐海县| 美姑县| 海南省| 宁国市| 体育| 连平县| 松原市| 申扎县| 慈溪市| 昌都县| 惠安县| 吉首市| 甘泉县| 扎兰屯市| 鹤山市| 都匀市|