DllImportAttribute Class
Assembly: mscorlib (in mscorlib.dll)
'Declaration <ComVisibleAttribute(True)> _ <AttributeUsageAttribute(AttributeTargets.Method, Inherited:=False)> _ Public NotInheritable Class DllImportAttribute Inherits Attribute 'Usage Dim instance As DllImportAttribute
/** @attribute ComVisibleAttribute(true) */ /** @attribute AttributeUsageAttribute(AttributeTargets.Method, Inherited=false) */ public final class DllImportAttribute extends Attribute
ComVisibleAttribute(true) AttributeUsageAttribute(AttributeTargets.Method, Inherited=false) public final class DllImportAttribute extends Attribute
Not applicable.
You can apply this attribute to methods.
The DllImportAttribute attribute provides the information needed to call a function exported from an unmanaged DLL. As a minimum requirement, you must supply the name of the DLL containing the entry point.
You apply this attribute directly to C# and C++ method definitions; however, the Visual Basic compiler emits this attribute when you use the Declare statement. For complex method definitions that include BestFitMapping, CallingConvention, ExactSpelling, PreserveSig, SetLastError, or ThrowOnUnmappableChar fields, you apply this attribute directly to Visual Basic method definitions.
Note JScript does not support this attribute. You can use C# or Visual Basic wrapper classes to access unmanaged API methods from JScript programs.
For additional information about using the platform invoke service to access functions in unmanaged DLLs, see Consuming Unmanaged DLL Functions.
Note: |
|---|
| The DllImportAttribute does not support marshaling of generic types. |
The following code example shows how to use the DllImportAttribute attribute to import the Win32 MessageBox function. The code example then calls the imported method.
Imports System Imports System.Runtime.InteropServices Module Example ' Use DllImport to import the Win32 MessageBox function. <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ 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
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: