共用方式為


MsgBox 範例

更新:2007 年 11 月

這個範例示範如何以傳值方式將字串型別當成 In 參數傳遞,以及何時使用 EntryPointCharSetExactSpelling 欄位。

MsgBox 範例使用下列 Unmanaged 函式,顯示其原始函式宣告:

  • 從 User32.dll 匯出的 MessageBox

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

在這個範例中,LibWrap 類別包含由 MsgBoxSample 類別呼叫的每一個 Unmanaged 函式的 Managed 原型。對於相同的 Unmanaged 函式,Managed 原型方法 MsgBox、MsgBox2 和 MsgBox3 有不同的宣告。

MsgBox2 的宣告會在訊息方塊中產生不正確的輸出,因為以 ANSI 指定的字元型別與進入點 (Entry Point) MessageBoxW (即 Unicode 函式的名稱) 不相符。MsgBox3 的宣告會在 EntryPointCharSetExactSpelling 欄位之間建立一個不相符。當呼叫時,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!" );
      }
   }
}

請參閱

工作

平台叫用技術範例

概念

封送處理字串

平台叫用資料型別

字串的預設封送處理

在 Managed 程式碼中建立原型

指定字元集