使用vs2008製做dll文件,生成動態連接庫

1.製做dll文件     vs2008中,File > New Project > Other Language > Win32 > Win32 Console Application. 輸入項目名稱:MyDll。點擊【OK】,【Next】,選擇dll單選按鈕,完成。     vs2008會自動生成一些代碼,不用管它。建立MyDll.h,代碼以下     MyDll.h 爲了提升可用性,寫了可迴帶值的函數供參考     Cpp代碼  收藏代碼         //MyDLL.h           extern "C" _declspec(dllexport) int Max(int a, int b);           extern "C" _declspec(dllexport) int Min(int a, int b);           extern "C" _declspec(dllexport) bool cim2scada(char* cimId, char** scadaId);            建立MyDll.cpp     Cpp代碼  收藏代碼         // MyDll.cpp : Defines the exported functions for the DLL application.          //          #include "stdafx.h"          #include"MyDll.h"           #include <iostream>                    int Max(int a, int b)           {               if(a>=b)return a;               else                   return b;           }           int Min(int a, int b)           {               if(a>=b)return b;               else                   return a;           }                      bool cim2scada(char* cimId, char** scadaId){              char *result;              if(cimId == NULL){                  printf("空指針!");                  return false;              }else if(strlen(cimId) == 0){                  printf("cimId無值!");                  return false;              }                        if(strcmp(cimId, "aaaaaa") == 0){                  result = "1000";              }else if(strcmp(cimId, "bbbbbb") == 0){                  result = "2000";              }              *scadaId = result;              printf("fun scadaId:%s\n", *scadaId);              return true;          }           編譯一下,到工程的DEBUG目錄,就能夠找到MyDll.dll文件 2.建立測試程序     vs2008中,File > New Project > Other Language > Win32 > Win32 Console Application. 輸入項目名稱:DllTest。點擊【OK】,【Next】,選擇console單選按鈕,去掉「預編譯頭」默認勾選項,完成。     Cpp代碼  收藏代碼         #include "windows.h"          #include <iostream>                    void main(){              typedef int(*pMax)(int a, int b);               typedef int(*pMin)(int a, int b);               typedef bool(*pcim2scada)(char* cimid, char** scada);              HINSTANCE HDLL;               HDLL=LoadLibrary(L"MyDll.dll");//加載動態連接庫MyDll.dll文件;               pMax Max=(pMax)GetProcAddress(HDLL,"Max");               pMin Min=(pMin)GetProcAddress(HDLL,"Min");               pcim2scada cim2scada=(pcim2scada)GetProcAddress(HDLL,"cim2scada");               int a = Max(5, 12);                             char cimId[] = "aaaaaa";              char * scadaId = "";              bool b = cim2scada(cimId, &scadaId);                        printf("Max結果爲%d",a);               printf("cimId:%s, scadaId:%s", cimId, scadaId);              FreeLibrary(HDLL);//卸載MyDll.dll文件;           }           編譯一下     拷貝MyDll.dll文件到DllTest項目的Debug目錄下     打開一個控制檯(cmd),進入DllTest > Debug目錄,運行DllTest.exe     輸出結果:     Cpp代碼  收藏代碼         fun scadaId : 1000          Max結果爲12          cimId:aaaaaa, scadaId:1000  ===================================================================================================== 這裏總結一下vs2008下建立dll並使用dll導出函數的方法,固然方法還有不少,找出一個本身比較習慣的方法就好! 一:生成DLL 1:建立DLL工程       文件->新建->項目->visual c++->win32->win32控制檯應用程序(win32項目也能夠)       填寫項目名稱MyDLL->肯定->下一步->DLL(附加選項 對空項目打鉤)->完成。       到這裏DLL工程就建立完畢了,下面新建兩個文件MyDLL.cpp和MyDLL.h。       MyDLL.cpp內容以下:     #include <iostream>     using namespace std;     #include "MyDLL.h"          int Add(int &a,int &b)     {         return a+b;      }       MyDLL.h內容以下:      #pragma once     #define DLL_EXPORT __declspec(dllexport)     extern "C" DLL_EXPORT int Add(int &a,int &b);      點擊生成,則dll文件就生成了,vs2008不能直接生成lib文件,這個時候就須要咱們在創建dll工程的時候 再新建一個def文件,默認生成而後從新生成就可以獲得lib文件了 2:調用DLL文件導出的接口函數        vs2008這個IDE很是好用,咱們這裏玩一下,在上面建立的DLL工程中,再新建一個項目來直接調用DLL導出函數。        右鍵解決方案->添加->新建項目->創建一個空的常規工程 testMyDLL,下面新建兩個文件testMyDLL.cpp和testMyDLL.h testMyDLL.cpp內容以下:     #include "testMyDLL.h"     #pragma comment(lib,"..\\debug\\MyDLL.lib")     #include <iostream>     using namespace std;          int main()     {         int a =3;         int b =2;         cout<<Add(a,b)<<endl;              getchar();     } testMyDLL.h內容以下:     #pragma once     #define DLL_EXPORT __declspec(dllexport)          extern "C" DLL_EXPORT int Add(int &a,int &b); 這裏須要注意testMyDLL.cpp文件中調用lib的這句話:     #pragma comment(lib,"..\\debug\\MyDLL.lib") 這裏須要指明lib所在的文件夾,固然咱們也能夠在生成dll的MyDLL工程中,指定lib和dll文件的輸出路徑,直接到testMyDLL工程下。 注:dll文件的操做有不少方法,待之後豐富。。。
相關文章
相關標籤/搜索