Dll 庫 DEF檔案詳解

2021-06-09 15:57:41 字數 1464 閱讀 8903

dll中匯出函式的宣告有兩種方式:

1、為在函式宣告中加上__declspec(dllexport),這裡不再舉例說明;

2、是採用模組定義(.def) 檔案宣告,.def檔案為鏈結器提供了有關被鏈結程式的匯出、屬性及其他方面的資訊。

首先建立 乙個dll程式,.cpp中

int __stdcall add(int numa, int numb)

int __stdcall sub(int numa, int numb)

然後建立乙個.def的檔案,在裡面加上

最後建立乙個測試程式:.cpp檔案如下:

#include

#include

using namespace std;

typedef int (__stdcall *fun)(int, int);

hinstance hinstance;

fun   fun;

int main()

cout << fun(1, 2) << endl;

freelibrary(hinstance);

return 0;}如:

library step01

exports

acrxentrypoint private @1

acrxgetapiversion private @2

說明:.def檔案的規則為:

(1)library語句說明.def檔案相應的dll為step01;

(2)exports語句後列出要匯出函式的名稱。可以在.def檔案中的匯出函式名後加@n,表示要匯出函式的序號為n(在進行函式呼叫時,這個序號將發揮其作用);

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

自己測試

// testdll.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include

#include

using namespace std;

typedef int(__stdcall *fun)( int ,int  );

hinstance hinstance;

fun fun;

int _tmain(int argc, _tchar* argv)

int __stdcall sub( int a, int b )

bool apientry dllmain( hmodule hmodule,

dword  ul_reason_for_call,

lpvoid lpreserved

)#ifdef _managed

#pragma managed(pop)

#endif

def檔案:

library "dlltest"

exports

add @1

sub

使用Def檔案匯出Dll檔案

本文介紹如何使用def檔案製作dll。模組定義 def 檔案是包含乙個或多個描述 dll 各種屬性的 module 語句的文字檔案。如果不使用 declspec dllexport 關鍵字匯出 dll 的函式,則 dll 需要 def 檔案。def 檔案必須至少包含下列模組定義語句 步驟 1.新建乙...

使用Def檔案匯出Dll檔案

本文介紹如何使用def檔案製作dll。模組定義 def 檔案是包含乙個或多個描述 dll 各種屬性的 module 語句的文字檔案。如果不使用 declspec dllexport 關鍵字匯出 dll 的函式,則 dll 需要 def 檔案。def 檔案必須至少包含下列模組定義語句 步驟 1.新建乙...

DLL的模組定義檔案( DEF)

動態連線庫函式或者成員的匯出可以用 declspec dllexport 來實現,比如為了匯出void sayhello 函式,則在dll檔案中這麼宣告 或者定義 declspec dllexport void sayhello 也可以不用 declspec dllexport 而採用.def檔案來...