使用 Unmanaged DLL 函式Consuming Unmanaged DLL Functions

平台叫用服務,可讓受控碼呼叫實作在動態連結程式庫 (DLL) 中的非受控函式,如 Windows API 中的函式。Platform invoke is a service that enables managed code to call unmanaged functions implemented in dynamic link libraries (DLLs), such as those in the Windows API. 它會找出並叫用匯出的函式,並且在需要的時候於交互操作界限之間封送處理其引數 (整數、 字串、 陣列、 結構和其他) 。It locates and invokes an exported function and marshals its arguments (integers, strings, arrays, structures, and so on) across the interoperation boundary as needed.

本節將介紹與取用非受控 DLL 函式相關聯的工作,並提供關於平台叫用的詳細資訊。This section introduces tasks associated with consuming unmanaged DLL functions and provides more information about platform invoke. 除了下列工作之外,還有一般考量以及提供其他資訊和範例的連結。In addition to the following tasks, there are general considerations and a link providing additional information and examples.

使用匯出的 DLL 函式To consume exported DLL functions

  1. 識別 DLL 中的函式Identify functions in DLLs.

    至少,您必須指定函式的名稱以及包含該函式之 DLL 的名稱。Minimally, you must specify the name of the function and name of the DLL that contains it.

  2. 建立類別以包裝 DLL 函式Create a class to hold DLL functions.

    您可以使用現有的類別、為每個 Unmanaged 函式建立個別的類別、或建立一個類別,其中包含一組相關的 Unmanaged 函式。You can use an existing class, create an individual class for each unmanaged function, or create one class that contains a set of related unmanaged functions.

  3. 在 Managed 程式碼中建立原型.Create prototypes in managed code.

    [Visual Basic] 搭配使用宣告陳述式與函式Lib 關鍵字。[Visual Basic] Use the Declare statement with the Function and Lib keywords. 在某些罕見的情況下,您可以搭配使用 DllImportAttribute共用函式關鍵字。In some rare cases, you can use the DllImportAttribute with the Shared Function keywords. 關於這種情況本節會於稍後加以說明。These cases are explained later in this section.

    [C#] 使用 DllImportAttribute 來識別 DLL 和函式。[C#] Use the DllImportAttribute to identify the DLL and function. 靜態外部修飾詞來標記方法。Mark the method with the static and extern modifiers.

    [C#] 使用 DllImportAttribute 來識別 DLL 和函式。[C++] Use the DllImportAttribute to identify the DLL and function. 外部 "C" 來標記包裝函式方法或函式。Mark the wrapper method or function with extern "C".

  4. 呼叫 DLL 函式Call a DLL function.

    在您的 Managed 類別上呼叫方法,如同您呼叫任何其他 Managed 方法。Call the method on your managed class as you would any other managed method. 傳遞結構實作回呼函式為特殊案例。Passing structures and implementing callback functions are special cases.

如需示範如何建構要與平台叫用搭配使用之 .NET 型宣告的範例,請參閱使用平台叫用封送處理資料For examples that demonstrate how to construct .NET-based declarations to be used with platform invoke, see Marshaling Data with Platform Invoke.

進一步了解平台叫用A closer look at platform invoke

平台叫用依賴中繼資料來找出被匯出的函式,並在執行階段封送處理其引數。Platform invoke relies on metadata to locate exported functions and marshal their arguments at run time. 下圖顯示這個程序。The following illustration shows this process.

顯示平台叫用呼叫的圖表。

當平台叫用呼叫 Unmanaged 函式時,它會依序執行下列動作:When platform invoke calls an unmanaged function, it performs the following sequence of actions:

  1. 找出包含該函式的 DLL。Locates the DLL containing the function.

  2. 將 DLL 載入到記憶體中。Loads the DLL into memory.

  3. 在記憶體中找出函式的位址,並將其引數推送至堆疊中,視需要封送處理資料。Locates the address of the function in memory and pushes its arguments onto the stack, marshaling data as required.

    注意

    僅在首次呼叫函式時尋找和載入 DLL,且尋找記憶體中的函式位址。Locating and loading the DLL, and locating the address of the function in memory occur only on the first call to the function.

  4. 將控制項傳輸至 Unmanaged 函式。Transfers control to the unmanaged function.

平台叫用會擲回由 Unmanaged 函式產生的例外狀況給 Managed 呼叫端。Platform invoke throws exceptions generated by the unmanaged function to the managed caller.

請參閱See also