MsgBox のサンプル

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

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

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

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

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

MsgBox2 に対する宣言により、メッセージ ボックス内に不正な出力が生成されます。その原因は、ANSI として指定した文字型が、Unicode 関数の名前であるエントリ ポイント MessageBoxW と一致しないからです。 MsgBox3 に対する宣言により、MsgBox3CharSet、および 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

関連項目