DllImportAttribute(String) Constructeur

Définition

Initialise une nouvelle instance de la classe DllImportAttribute avec le nom de la DLL contenant la méthode à importer.

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)

Paramètres

dllName
String

Nom de la DLL contenant la méthode non managée. Cela peut inclure le nom complet d'un assembly, si la DLL est incluse dans un assembly.

Exemples

L’exemple de code suivant montre comment utiliser l’attribut DllImportAttribute pour importer la fonction Win32 MessageBox . L’exemple de code appelle ensuite la méthode importée.

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

Remarques

Si un fichier DLL non managé est inclus dans un assembly, par exemple, à l’aide de l’éditeur de liens ou de l’option du /linkresource compilateur, vous pouvez spécifier le nom d’affichage de l’assembly dans le cadre de dllName. Par exemple, si une DLL non managée nommée unmanaged.dll est incluse dans un assembly managé nommé MyAssembly, l’attribut peut être spécifié comme indiqué dans le code suivant.

[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

S’applique à