DLL中導出函數的兩種方式

DLL中導出函數的兩種方式(dllexport與.def文件)(2009-03-06 11:34:58)

DLL中導出函數的聲明有兩種方式:

一種方式是:在函數聲明中加上__declspec(dllexport);
另外一種方式是:採用模塊定義(.def)文件聲明,(.def)文件爲鏈接器提供了有關被鏈接程序的導出、屬性及其他方面的信息。

方式一:在函數聲明中加上__declspec(dllexport)
/// 在動態鏈接庫程序中
/// 聲明動態鏈接庫(**.dll)的對外接口函數TestFuction
extern "C" __declspec(dllexport) int TestFuction(int nType,char *strPath,std::vector<string> &vecData)
{
   ////do anything here////
   return 0;
}


/// 在外部希望調用動態鏈接庫的程序中
/// 加載動態鏈接庫(**.dll)並調用其對外接口TestFuction
void func()
{
  //typedef與函數TestFuction類型相同的函數指針爲TESTDLL
  typedef int (_cdecl * TESTDLL)(int nType,char *strPath,std::vector<string> &vecData);
  HINSTANCE hmod;
  //加載動態鏈接庫**.dll
  hmod =::LoadLibrary(_TEXT("dll相對路徑//**.dll"));
  if(NULL == hmod)
  {
     TRACE("加載**.dll失敗");
  }
  //定義一個與函數TestFuction類型相同的函數指針lpproc
  TESTDLL lpproc;
  //搜索**.dll中函數名爲TestFuction的對外接口
  lpproc = (TESTDLL)GetProcAddress (hmod,"TestFuction");
  //如果搜索成功
  if(NULL != lpproc)
  {
     int nType = 0;
     char* strPath = "Data";
     std::vector<string> vecData;
     //通過函數指針lpproc調用**.dll的接口函數TestFuction
     int nResult = (*lpproc)(nType,strPath,vecData);
  }
  //...
  //在恰當的時候釋放動態鏈接庫**.dll
  FreeLibrary(hmod);
}


方式二:採用模塊定義(.def)文件聲明
首先創建 一個DLL程序(DllTestDef)
在*.cpp中
int __stdcall Add(int numa, int numb)
{
     return (numa + numb);
}

int __stdcall Sub(int numa, int numb)
{
     return (numa - numb);
}

然後創建一個.def的文件,在裏面加上

;DllTestDef.lib : 導出DLL函數
;作者:----
LIBRARY DllTestDef
EXPORTS
Add @ 1
Sub @ 2

最後創建一個測試程序:.cpp文件如下:
#include <iostream>
#include <windows.h>

using namespace std;

typedef int (__stdcall *FUN)(int, int);
HINSTANCE hInstance;
FUN   fun;

int main()
{
       hInstance = LoadLibrary("DLLTestDef.dll");
       if(!hInstance)
           cout << "Not Find this Dll" << endl;
       fun = (FUN)GetProcAddress(hInstance, MAKEINTRESOURCE(1));
       if (!fun)
       {
              cout << "not find this fun" << endl;
       }
       cout << fun(1, 2) << endl;
       FreeLibrary(hInstance);
       return 0;
}

說明:
.def文件的規則爲:

(1)LIBRARY語句說明.def文件相應的DLL;

(2)EXPORTS語句後列出要導出函數的名稱。可以在.def文件中的導出函數名後加@n,表示要導出函數的序號爲n(在進行函數調用時,這個序號將發揮其作用);

(3).def 文件中的註釋由每個註釋行開始處的分號 (;) 指定,且註釋不能與語句共享一行。

(4)使用__declspec(dllexport)和使用.def文件是有區別的。

如果你的DLL是提供給VC用戶使用的,你只需要把編譯DLL時產生的.lib提供給用戶,
它可以很輕鬆地調用你的DLL。但是如果你的DLL是供VB、PB、Delphi用戶使用的,那麼會產生一個小麻煩。
因爲VC++編譯器對於__declspec(dllexport)聲明的函數會進行名稱轉換,如下面的函數:
__declspec(dllexport) int __stdcall Add()
會轉換爲Add@0,這樣你在VB中必須這樣聲明:
Declare Function Add Lib "DLLTestDef.dll" Alias "Add@0" () As Long
@後面的數由於參數類型不同而可能不同。這顯然不太方便。所以如果要想避免這種轉換,就要使用.def文件方式導出函數了。

發佈了20 篇原創文章 · 獲贊 4 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章