Share via


MsgBox-Beispiel

Dieses Beispiel demonstriert, wie Zeichenfolgentypen durch einen Wert als In-Parameter übergeben werden und wann das EntryPoint-Feld, das CharSet-Feld und das ExactSpelling-Feld zu verwenden sind.

Das MsgBox-Beispiel verwendet die folgende nicht verwaltete Funktion, die zusammen mit ihrer ursprünglichen Funktionsdeklaration aufgeführt wird:

  • MessageBox aus User32.dll exportiert.

    int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, 
       UINT uType);
    

In diesem Beispiel enthält die LibWrap-Klasse einen verwalteten Prototyp für jede nicht verwaltete Funktion, die durch die MsgBoxSample-Klasse aufgerufen wird. Die verwalteten Prototypmethoden MsgBox, MsgBox2 und MsgBox3 verfügen über unterschiedliche Deklarationen für ein und dieselbe nicht verwaltete Funktion.

Die Deklaration für MsgBox2 erzeugt eine falsche Ausgabe im Meldungsfeld, da der als ANSI angegebene Zeichentyp nicht mit dem Einstiegspunkt MessageBoxW übereinstimmt, der dem Namen der Unicode-Funktion entspricht. Die Deklaration für MsgBox3 erzeugt einen Übereinstimmungsfehler zwischen dem EntryPoint-Feld, dem CharSet-Feld und dem ExactSpelling-Feld. Die MsgBox3 -Methode löst bei ihrem Aufruf eine Ausnahme aus. Ausführliche Informationen zur Benennung von Zeichenfolgen und zum Namensmarshallen finden Sie unter Angeben eines Zeichensatzes.

Deklarieren von Prototypen

Public Class LibWrap
   ' Declares managed prototypes for unmanaged functions.
   Declare Auto Function MsgBox Lib "User32.dll" Alias "MessageBox" ( _
       ByVal hWnd As Integer, ByVal txt As String, ByVal caption As String, _
       ByVal typ As Integer ) As Integer

   ' Causes incorrect output in the message window.
   Declare Ansi Function MsgBox2 Lib "User32.dll" Alias "MessageBoxW" ( _
       ByVal hWnd As Integer, ByVal txt As String, ByVal caption As String, _
       ByVal type As Integer ) As Integer

   ' Causes an exception to be thrown.
   ' ExactSpelling is True by default in when
   ' Ansi or Unicode is used.
   Declare Ansi Function MsgBox3 Lib "User32.dll" Alias "MessageBox" ( _
       ByVal hWnd As Integer, ByVal txt As String, ByVal caption As String, _
       ByVal typ As Integer ) As Integer
End Class
public class LibWrap
{
    // Declares managed prototypes for unmanaged functions.
    [DllImport( "User32.dll", EntryPoint="MessageBox",
        CharSet=CharSet.Auto)]
    public static extern int MsgBox(int hWnd, string text, string caption,
      uint type);

    // Causes incorrect output in the message window.
    [DllImport( "User32.dll", EntryPoint="MessageBoxW",
        CharSet=CharSet.Ansi )]
    public static extern int MsgBox2(int hWnd, string text,
       string caption, uint type);

    // Causes an exception to be thrown. EntryPoint, CharSet, and
    // ExactSpelling fields are mismatched.
    [DllImport( "User32.dll", EntryPoint="MessageBox",
        CharSet=CharSet.Ansi, ExactSpelling=true )]
    public static extern int MsgBox3(int hWnd, string text,
        string caption, uint type);
}
public ref class LibWrap
{
public:
    // Declares managed prototypes for unmanaged functions.
    [DllImport( "User32.dll", EntryPoint="MessageBox",
        CharSet=CharSet::Auto)]
    static int MsgBox(int hWnd, String^ text, String^ caption,
      unsigned int type);

    // Causes incorrect output in the message window.
    [DllImport( "User32.dll", EntryPoint="MessageBoxW",
        CharSet=CharSet::Ansi )]
    static int MsgBox2(int hWnd, String^ text,
       String^ caption, unsigned int type);

    // Causes an exception to be thrown. EntryPoint, CharSet, and
    // ExactSpelling fields are mismatched.
    [DllImport( "User32.dll", EntryPoint="MessageBox",
        CharSet=CharSet::Ansi, ExactSpelling=true )]
    static int MsgBox3(int hWnd, String^ text,
        String^ caption, unsigned int type);
};

Aufrufen von Funktionen

Public Class MsgBoxSample
    Public Shared Sub Main()
        LibWrap.MsgBox(0, "Correct text", "MsgBox Sample", 0)
        LibWrap.MsgBox2(0, "Incorrect text", "MsgBox Sample", 0)

        Try
            LibWrap.MsgBox3(0, "No such function", "MsgBox Sample", 0)
        Catch e As EntryPointNotFoundException
            Console.WriteLine("EntryPointNotFoundException thrown as expected!")
        End Try
    End Sub
End Class
public class MsgBoxSample
{
    public static void Main()
    {
        LibWrap.MsgBox(0, "Correct text", "MsgBox Sample", 0);
        LibWrap.MsgBox2(0, "Incorrect text", "MsgBox Sample", 0);

        try
        {
            LibWrap.MsgBox3(0, "No such function", "MsgBox Sample", 0);
        }
        catch(EntryPointNotFoundException)
        {
           Console.WriteLine("EntryPointNotFoundException thrown as expected!");
        }
    }
}
public class MsgBoxSample
{
public:
    static void Main()
    {
        LibWrap::MsgBox(0, "Correct text", "MsgBox Sample", 0);
        LibWrap::MsgBox2(0, "Incorrect text", "MsgBox Sample", 0);

        try
        {
            LibWrap::MsgBox3(0, "No such function", "MsgBox Sample", 0);
        }
        catch (EntryPointNotFoundException^)
        {
           Console::WriteLine("EntryPointNotFoundException thrown as expected!");
        }
    }
};

Siehe auch

Konzepte

Marshallen von Zeichenfolgen

Datentypen für den Plattformaufruf

Standardmäßiges Marshalling für Zeichenfolgen

Erstellen von Prototypen in verwaltetem Code

Weitere Ressourcen

Specifying a Character Set