DllImportAttribute(String) 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化一個新的類別實例 DllImportAttribute ,並以包含匯入方法的 DLL 名稱。
public:
DllImportAttribute(System::String ^ dllName);
public DllImportAttribute(string dllName);
new System.Runtime.InteropServices.DllImportAttribute : string -> System.Runtime.InteropServices.DllImportAttribute
Public Sub New (dllName As String)
參數
- dllName
- String
包含未管理方法的 DLL 名稱。 在 .NET Framework 中,若 DLL 包含在組件中,則可包含組合語言顯示名稱。
範例
以下程式碼範例展示了如何使用屬性 DllImportAttribute 來匯入 Win32 MessageBox 函式。 程式碼範例接著呼叫導入的方法。
using System;
using System.Runtime.InteropServices;
class Example
{
// Use DllImport to import the Win32 MessageBox function.
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
static void Main()
{
// Call the MessageBox function using platform invoke.
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}
Imports System.Runtime.InteropServices
Module Example
' Use DllImport to import the Win32 MessageBox function.
<DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
Function MessageBox(ByVal hwnd As IntPtr, ByVal t As String, ByVal caption As String, ByVal t2 As UInt32) As Integer
End Function
Sub Main()
' Call the MessageBox function using platform invoke.
MessageBox(New IntPtr(0), "Hello World!", "Hello Dialog", 0)
End Sub
End Module
備註
.NET僅限框架:若組合語言中包含未管理的 DLL 檔案,例如透過連結器或 /linkresource 編譯器選項,你可以將組合語言的顯示名稱指定為 dllName 的一部分。 例如,若一個名為 的管理組譯器中包含一個未unmanaged.dllMyAssembly受管理的 DLL 名稱,則該屬性可能如以下程式碼所示指定。
[DllImport("unmanaged.dll, MyAssembly, Version= 1.0.0.0,"
"Culture=neutral, PublicKeyToken=a77e0ba5eab10125")]
int SomeFuncion1(int parm);
[DllImport("unmanaged.dll, MyAssembly, Version= 1.0.0.0," +
"Culture=neutral, PublicKeyToken=a77e0ba5eab10125")]
internal static extern int SomeFuncion1(int parm);
<DllImport("unmanaged.dll, MyAssembly, Version= 1.0.0.0," +
"Culture=neutral, PublicKeyToken=a77e0ba5eab10125")>
Friend Shared Function DummyFuncion1(parm As Integer) As Integer
End Function