Condividi tramite


Esempio di MsgBox

Aggiornamento: novembre 2007

Nell'esempio viene illustrato come passare i tipi stringa per valore come parametri in e quando utilizzare i campi EntryPoint, CharSet ed ExactSpelling.

Nell'esempio di MsgBox viene utilizzata la seguente funzione non gestita, illustrata con la dichiarazione di funzione originale:

  • MessageBox esportata da User32.dll.

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

In questo esempio, la classe LibWrap contiene un prototipo gestito per ciascuna funzione non gestita chiamata dalla classe MsgBoxSample. I metodi del prototipo gestito MsgBox, MsgBox2 e MsgBox3 hanno dichiarazioni diverse per la stessa funzione non gestita.

La dichiarazione per MsgBox2 produce output errato nella finestra di messaggio poiché il tipo di carattere, specificato come ANSI, non corrisponde alla funzione MessageBoxW del punto di ingresso, il cui nome è Unicode. La dichiarazione per MsgBox3 genera una mancata corrispondenza tra i campi EntryPoint, CharSet ed ExactSpelling. Quando viene chiamato, MsgBox3 genera un'eccezione. Per informazioni dettagliate sulla denominazione delle stringhe e il marshalling dei nomi, vedere Specifica di un set di caratteri.

Il codice sorgente per gli esempi di codice riportati di seguito è fornito dall'Esempio di tecnologia di richiamo piattaforma di .NET Framework.

Dichiarazione dei prototipi

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, _
   yVal 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 Visual Basic 2005 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 );
}

Chiamata delle funzioni

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!" );
      }
   }
}

Vedere anche

Attività

Esempio di tecnologia di richiamo piattaforma

Concetti

Marshalling di stringhe

Tipi di dati del richiamo piattaforma

Marshalling predefinito per le stringhe

Creazione di prototipi nel codice gestito

Specifica di un set di caratteri