1.創(chuàng)建dll工程

          以創(chuàng)建win32 dll程序?yàn)槔话阌袃煞N方式:

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

          #ifdef __cplusplus 

          #define EXPORT extern "C"__declspec(dllexport)

          #else

          #define EXPORT __declspec(dllexport)

          #endif

          EXPORT int HelloWorld()

          {

          cout << "hello world" << endl;

          return 0;

          }

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

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

          LIBRARY "computer"

          EXPORTS

          add PRIVATE

          而在代碼里只需要用:

          DllMain 前面加上你自己要導(dǎo)出的函數(shù):

          int add(int x,int y)

          return(x + y);

          }

          而在使用的時(shí)候:

          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.上面是最簡(jiǎn)單的方式,弊端別人可以輕易的使用我們的dll。

          如果我們要想著封裝下免得被其他人隨意使用,于是就有了導(dǎo)出函數(shù)指針,創(chuàng)建對(duì)象的思路了...具體闡述如下:

          創(chuàng)建一個(gè)接口文件,供使用者參考。dll里面提供導(dǎo)出函數(shù)指針,創(chuàng)建這個(gè)接口的現(xiàn)實(shí)類對(duì)象,利用這個(gè)對(duì)象就可以使用其中的功能了。

          a ) 創(chuàng)建一個(gè)publish文件(提供給使用者)

          比如: computer_def.h

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

          當(dāng)然不要忘記書寫你的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;

          }


          完成接口實(shí)現(xiàn)。提供導(dǎo)出函數(shù)。

          在使用的工程中,記得引入頭文件 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;
                  }

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

          使用的時(shí)候:

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

             pComputer->del();


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

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊(cè)

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914107
          • 排名 - 40

          最新評(píng)論

          主站蜘蛛池模板: 清徐县| 庄河市| 稷山县| 武穴市| 满城县| 怀仁县| 仪陇县| 庆云县| 永春县| 丰台区| 冕宁县| 云安县| 禄劝| 保德县| 张家口市| 荆州市| 九龙坡区| 澎湖县| 铁力市| 建瓯市| 襄樊市| 祁门县| 德庆县| 鹰潭市| 谷城县| 诸暨市| 临泽县| 泸州市| 芜湖县| 固安县| 江安县| 天台县| 东兴市| 潍坊市| 名山县| 隆林| 武乡县| 铁岭市| 蓝山县| 宝兴县| 定西市|