Como: chamar APIs do Windows (Visual Basic)

This example defines and calls the MessageBox function in user32.dll and then passes a string to it.

Exemplo

Este exemplo de código também está disponível como um trecho de código IntelliSense. In the code snippet picker, it is located in Visual Basic Language. For more information, see Como: Inserir trechos de código de IntelliSense.

' 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

Compilando o código

This example requires:

  • A reference to the System namespace.

Programação robusta

The following conditions may cause an exception:

  • The method is not static, is abstract, or has been previously defined. The parent type is an interface, or the length of name or dllName is zero. (ArgumentException)

  • The name or dllName is Nothing. (ArgumentNullException)

  • The containing type has been previously created using CreateType. (InvalidOperationException)

Consulte também

Tarefas

Passo a passo: Chamando APIs do Windows (Visual Basic)

Conceitos

Uma análise detalhada da plataforma Invoke

Exemplos de invocação de plataforma

Consumir funções DLL não gerenciada

Definindo um método com reflexão emitir.

Outros recursos

Interoperabilidade COM (Visual Basic)