MsgBox のサンプル

このサンプルでは、文字列型を In パラメータとして値渡しする方法と、EntryPointCharSet、および ExactSpelling の各フィールドを使用する場合について説明します。

MsgBox のサンプルで使用するアンマネージ関数とその関数宣言を次に示します。

  • User32.dll からエクスポートされる MessageBox

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

このサンプルでは、LibWrap クラスの中には、MsgBoxSample クラスによって呼び出される各アンマネージ関数に関するマネージ プロトタイプが含まれます。マネージ プロトタイプ メソッドの MsgBoxMsgBox2、および MsgBox3 は、同じアンマネージ関数に対して異なる宣言を持ちます。

MsgBox2 に対する宣言により、メッセージ ボックス内に不正な出力が生成されます。その原因は、ANSI として指定した文字型が、Unicode 関数の名前であるエントリ ポイント MessageBoxW と一致しないからです。MsgBox3 に対する宣言により、EntryPointCharSet、および ExactSpelling の各フィールド間に不一致が発生します。MsgBox3 を呼び出すと例外がスローされます。文字列の名前付けと名前のマーシャリングの詳細については、「文字セットの指定」を参照してください。

次のコード例のソース コードは、.NET Framework「プラットフォーム呼び出しの技術サンプル」で提供されています。

プロトタイプの宣言

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

関数の呼び出し

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

参照

処理手順

プラットフォーム呼び出しの技術サンプル

概念

文字列のマーシャリング
プラットフォーム呼び出しのデータ型
文字列に対する既定のマーシャリング
マネージ コードでのプロトタイプの作成
文字セットの指定