Platform Invoke Examples


The following examples demonstrate how to define and call the MessageBox function in User32.dll, passing a simple string as an argument. In the examples, the DllImportAttribute.CharSet field is set to Auto to let the target platform determine the character width and string marshaling.

Imports System.Runtime.InteropServices

Public Class Win32
    Declare Auto Function MessageBox Lib "user32.dll" _
       (ByVal hWnd As Integer, ByVal txt As String, _
       ByVal caption As String, ByVal Typ As Integer) As IntPtr
End Class

Public Class HelloWorld    
    Public Shared Sub Main()
        Win32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0)
    End Sub
End Class

For additional examples, see Marshaling Data with Platform Invoke.

Creating Prototypes in Managed Code
Specifying a Character Set