指定進入點Specifying an Entry Point

進入點可識別函式在 DLL 中的位置。An entry point identifies the location of a function in a DLL. 在 Managed 專案中,目標函式的原始名稱或序數進入點可跨越交互操作界限識別該函式。Within a managed project, the original name or ordinal entry point of a target function identifies that function across the interoperation boundary. 此外,您可以將進入點對應到不同的名稱,有效地重新命名函式。Further, you can map the entry point to a different name, effectively renaming the function.

以下是重新命名 DLL 函式的可能原因清單:The following is a list of possible reasons to rename a DLL function:

  • 避免使用區分大小寫的 API 函式名稱To avoid using case-sensitive API function names

  • 符合現有的命名標準To comply with existing naming standards

  • 容納接受不同資料類型 (藉由宣告相同 DLL 函式的多個版本) 的函式To accommodate functions that take different data types (by declaring multiple versions of the same DLL function)

  • 簡化使用包含 ANSI 和 Unicode 版本的 APITo simplify using APIs that contain ANSI and Unicode versions

本主題示範如何在 Managed 程式碼中重新命名 DLL 函式。This topic demonstrates how to rename a DLL function in managed code.

在 Visual Basic 中重新命名函式Renaming a Function in Visual Basic

Visual Basic 使用 Function 關鍵字在 Declare 陳述式中設定 DllImportAttribute.EntryPoint 欄位。Visual Basic uses the Function keyword in the Declare statement to set the DllImportAttribute.EntryPoint field. 下列範例示範基本宣告。The following example shows a basic declaration.

Friend Class NativeMethods
    Friend Declare Auto Function MessageBox Lib "user32.dll" (
        ByVal hWnd As IntPtr,
        ByVal lpText As String,
        ByVal lpCaption As String,
        ByVal uType As UInteger) As Integer
End Class

您可以將 MessageBox 進入點取代為 MsgBox,方法是在您的定義中包含 Alias 關鍵字,如下列範例所示。You can replace the MessageBox entry point with MsgBox by including the Alias keyword in your definition, as shown in the following example. 在這兩個範例中,Auto 關鍵字讓您不需要指定進入點的字元集版本。In both examples the Auto keyword eliminates the need to specify the character-set version of the entry point. 如需選取字元集的詳細資訊,請參閱指定字元集For more information about selecting a character set, see Specifying a Character Set.

Friend Class NativeMethods
    Friend Declare Auto Function MsgBox _
        Lib "user32.dll" Alias "MessageBox" (
        ByVal hWnd As IntPtr,
        ByVal lpText As String,
        ByVal lpCaption As String,
        ByVal uType As UInteger) As Integer
End Class

在 C# 和 C++ 中重新命名函式Renaming a Function in C# and C++

您可以使用 DllImportAttribute.EntryPoint 欄位來依名稱或序數指定 DLL 函式。You can use the DllImportAttribute.EntryPoint field to specify a DLL function by name or ordinal. 如果方法定義中的函式名稱與 DLL 中的進入點相同,則不必明確地以 EntryPoint 欄位來識別函式。If the name of the function in your method definition is the same as the entry point in the DLL, you do not have to explicitly identify the function with the EntryPoint field. 否則,請使用其中一種下列屬性形式來表示名稱或序數:Otherwise, use one of the following attribute forms to indicate a name or ordinal:

[DllImport("DllName", EntryPoint = "Functionname")]
[DllImport("DllName", EntryPoint = "#123")]

請注意,您必須在序數前面加上井字號 (#)。Notice that you must prefix an ordinal with the pound sign (#).

下列範例示範如何使用 EntryPoint 欄位,將程式碼中的 MessageBoxA 取代為 MsgBoxThe following example demonstrates how to replace MessageBoxA with MsgBox in your code by using the EntryPoint field.

using System;
using System.Runtime.InteropServices;

internal static class NativeMethods
{
    [DllImport("user32.dll", EntryPoint = "MessageBoxA")]
    internal static extern int MessageBox(
        IntPtr hWnd, string lpText, string lpCaption, uint uType);
}
using namespace System;
using namespace System::Runtime::InteropServices;

typedef void* HWND;
[DllImport("user32", EntryPoint = "MessageBoxA")]
extern "C" int MsgBox(
    HWND hWnd, String* lpText, String* lpCaption, unsigned int uType);

另請參閱See also