如何:调用 Windows API (Visual Basic)

此示例定义并调用 user32.dll 中的 MessageBox 函数,然后将字符串传递给它。

示例

' Defines the MessageBox function.
Public Class Win32
    Declare Auto Function MessageBox Lib "user32.dll" (
        ByVal hWnd As Integer, ByVal txt As String,
        ByVal caption As String, ByVal Type As Integer
        ) As Integer
End Class

' Calls the MessageBox function.
Public Class DemoMessageBox
    Public Shared Sub Main()
        Win32.MessageBox(0, "Here's a MessageBox", "Platform Invoke Sample", 0)
    End Sub
End Class

编译代码

此示例需要:

  • System 命名空间的引用。

可靠编程

以下情况可能会导致异常:

另请参阅