MsgBox 示例

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

MsgBox 示例使用以下未托管的函数(与其原始函数声明一同显示):

  • 从 User32.dll 导出的 MessageBox。

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

在此示例中,NativeMethods 类包含 MsgBoxSample 类调用的每一个未托管的函数的托管原型。 托管原型方法 MsgBoxMsgBox2MsgBox3 对于相同的未托管的函数有不同的声明。

由于指定为 ANSI 的字符类型与 Unicode 函数名称的入口点 MessageBoxW 不匹配,MsgBox2 的声明会在消息框中生成不正确的输出。 MsgBox3 的声明会在 EntryPoint、CharSet 和 ExactSpelling 字段之间创建不匹配。 调用时,MsgBox3 会引发异常。 有关字符串命名和名称封送处理的详细信息,请参阅指定字符集

声明原型

private ref class NativeMethods
{
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);
};
internal static class NativeMethods
{
    // Declares managed prototypes for unmanaged functions.
    [DllImport("User32.dll", EntryPoint = "MessageBox",
        CharSet = CharSet.Auto)]
    internal static extern int MsgBox(
        IntPtr hWnd, string lpText, string lpCaption, uint uType);

    // Causes incorrect output in the message window.
    [DllImport("User32.dll", EntryPoint = "MessageBoxW",
        CharSet = CharSet.Ansi)]
    internal static extern int MsgBox2(
        IntPtr hWnd, string lpText, string lpCaption, uint uType);

    // Causes an exception to be thrown. EntryPoint, CharSet, and
    // ExactSpelling fields are mismatched.
    [DllImport("User32.dll", EntryPoint = "MessageBox",
        CharSet = CharSet.Ansi, ExactSpelling = true)]
    internal static extern int MsgBox3(
        IntPtr hWnd, string lpText, string lpCaption, uint uType);
}
Friend Class NativeMethods
    ' Declares managed prototypes for unmanaged functions.
    Friend Declare Auto Function MsgBox Lib "User32.dll" Alias "MessageBox" (
        ByVal hWnd As IntPtr, ByVal lpText As String, ByVal lpCaption As String,
        ByVal uType As UInteger) As Integer

    ' Causes incorrect output in the message window.
    Friend Declare Ansi Function MsgBox2 Lib "User32.dll" Alias "MessageBoxW" (
        ByVal hWnd As IntPtr, ByVal lpText As String, ByVal lpCaption As String,
        ByVal uType As UInteger) As Integer

    ' Causes an exception to be thrown.
    ' ExactSpelling is True by default when Ansi or Unicode is used.
    Friend Declare Ansi Function MsgBox3 Lib "User32.dll" Alias "MessageBox" (
        ByVal hWnd As IntPtr, ByVal lpText As String, ByVal lpCaption As String,
        ByVal uType As UInteger) As Integer
End Class

调用函数

public class MsgBoxSample
{
public:
    static void Main()
    {
        NativeMethods::MsgBox(0, "Correct text", "MsgBox Sample", 0);
        NativeMethods::MsgBox2(0, "Incorrect text", "MsgBox Sample", 0);

        try
        {
            NativeMethods::MsgBox3(0, "No such function", "MsgBox Sample", 0);
        }
        catch (EntryPointNotFoundException^)
        {
            Console::WriteLine("EntryPointNotFoundException thrown as expected!");
        }
    }
};
public class MsgBoxSample
{
    public static void Main()
    {
        NativeMethods.MsgBox(0, "Correct text", "MsgBox Sample", 0);
        NativeMethods.MsgBox2(0, "Incorrect text", "MsgBox Sample", 0);

        try
        {
            NativeMethods.MsgBox3(0, "No such function", "MsgBox Sample", 0);
        }
        catch (EntryPointNotFoundException)
        {
            Console.WriteLine($"{nameof(EntryPointNotFoundException)} thrown as expected!");
        }
    }
}
Public Class MsgBoxSample
    Public Shared Sub Main()
        NativeMethods.MsgBox(0, "Correct text", "MsgBox Sample", 0)
        NativeMethods.MsgBox2(0, "Incorrect text", "MsgBox Sample", 0)

        Try
            NativeMethods.MsgBox3(0, "No such function", "MsgBox Sample", 0)
        Catch e As EntryPointNotFoundException
            Console.WriteLine($"{NameOf(EntryPointNotFoundException)} thrown as expected!")
        End Try
    End Sub
End Class

请参阅