Partager via


MsgBox, exemple

Mise à jour : novembre 2007

Cet exemple montre comment passer des types chaîne par valeur comme paramètres en entrée et quand utiliser les champs EntryPoint, CharSet et ExactSpelling.

L'exemple MsgBox utilise la fonction non managée suivante, illustrée avec sa déclaration de fonction d'origine :

  • MessageBox exportée à partir de User32.dll.

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

Dans cet exemple, la classe LibWrap contient un prototype managé pour chaque fonction non managée appelée par la classe MsgBoxSample. Les méthodes de prototype managé MsgBox, MsgBox2, et MsgBox3 possèdent différentes déclarations pour la même fonction non managée.

La déclaration pour MsgBox2 produit un résultat incorrect dans le message car le type caractère, spécifié comme étant ANSI, est incompatible avec le point d'entrée MessageBoxW, qui est le nom de la fonction Unicode. La déclaration pour MsgBox3 crée une incompatibilité entre les champs EntryPoint, CharSet et ExactSpelling. Lorsqu'elle est appelée, MsgBox3 lève une exception. Pour plus d'informations sur l'affectation de noms de chaînes et le marshaling de nom, consultez Spécification d'un jeu de caractères.

Le code source pour les exemples de code suivants est fourni par Appel de plate-forme, exemple de technologie du .NET Framework.

Déclaration de prototypes

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

Appel de fonctions

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

Voir aussi

Tâches

Appel de plate-forme, exemple de technologie

Concepts

Marshaling de chaînes

Types de données d'appel de plate-forme

Marshaling par défaut pour les chaînes

Création de prototypes dans du code managé

Spécification d'un jeu de caractères