MsgBox 示例

此示例演示如何通过值将字符串类型作为 In 参数传递以及何时使用 EntryPointCharSetExactSpelling 字段。

MsgBox 示例使用以下非托管函数(这里同时显示其原始函数声明):

  • 从 User32.dll 导出的 MessageBox

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

在该示例中,LibWrap 类包含 MsgBoxSample 类调用的每一个非托管函数的托管原型。 托管原型方法 MsgBox、MsgBox2 和 MsgBox3 对于同一个非托管函数具有不同的声明。

MsgBox2 的声明将在消息框中产生不正确的输出,原因是被指定为 ANSI 的字符类型与按 Unicode 函数命名的入口点 MessageBoxW 不匹配。 MsgBox3 的声明将在 EntryPointCharSetExactSpelling 字段之间产生不匹配现象。 在调用时,MsgBox3 将引发异常。 有关字符串命名和名称封送处理的详细信息,请参见指定字符集

声明原型

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

调用函数

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

请参见

概念

封送处理字符串

平台调用数据类型

字符串的默认封送处理

在托管代码中创建原型

其他资源

Specifying a Character Set