ページ 11

UnmanagedType.Boolとboolの違い

Posted: 2017年1月04日(水) 15:50
by ちゃん太
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool EnumWindows(EnumWindowsDelegate lpEnumFunc,
IntPtr lparam);

[return: MarshalAs(UnmanagedType.Bool)]って必要ですか?

Re: UnmanagedType.Boolとboolの違い

Posted: 2017年1月04日(水) 18:29
by Math
これはマーシャリングといってC/C++とC#の言語間の型の違いを調整するためですよ。UnmanagedType.BoolがC/C++の「論理型」でbool がC#の「論理型」です。ここhttp://www.cactussoft.co.jp/Sarbo/divManageMarsh0.htmlに説明があります。
>[return: MarshalAs(UnmanagedType.Bool)]って必要ですか?
こういう時には必要です。(サイトのサンプル)。VS2008で動作確認しました。
(長い例しかなくてこれが短いコードのほうです。分からない事が有れば聞いて下さい)

コード:

using System;using System.Runtime.InteropServices;using System.Text;
public class Program
{
    public static void Main()
    {
        //ウィンドウを列挙する
        EnumWindows(new EnumWindowsDelegate(EnumWindowCallBack), IntPtr.Zero);
        Console.ReadLine();
    }
    public delegate bool EnumWindowsDelegate(IntPtr hWnd, IntPtr lparam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public extern static bool EnumWindows(EnumWindowsDelegate lpEnumFunc,
        IntPtr lparam);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int GetWindowText(IntPtr hWnd,
        StringBuilder lpString, int nMaxCount);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int GetClassName(IntPtr hWnd,
        StringBuilder lpClassName, int nMaxCount);

    private static bool EnumWindowCallBack(IntPtr hWnd, IntPtr lparam)
    {
        //ウィンドウのタイトルの長さを取得する
        int textLen = GetWindowTextLength(hWnd);
        if (0 < textLen)
        {
            //ウィンドウのタイトルを取得する
            StringBuilder tsb = new StringBuilder(textLen + 1);
            GetWindowText(hWnd, tsb, tsb.Capacity);

            //ウィンドウのクラス名を取得する
            StringBuilder csb = new StringBuilder(256);
            GetClassName(hWnd, csb, csb.Capacity);

            //結果を表示する
            Console.WriteLine("クラス名:" + csb.ToString());
            Console.WriteLine("タイトル:" + tsb.ToString());
        }
        //すべてのウィンドウを列挙する
        return true;
    }
}

Re: UnmanagedType.Boolとboolの違い

Posted: 2017年1月04日(水) 20:02
by ちゃん太
Mathさんが書かれたコードの場合、てかあるサイトのコードで僕もそれを見ていましたけど、その場合MarshalAS~を書かないとエラーになるんですか?

Re: UnmanagedType.Boolとboolの違い

Posted: 2017年1月04日(水) 20:24
by Math
もちろんです。実際にやってみると”エラー 1 ID がありません。”となります。VisualStudioは2015を使っていますか?(私はVS2015です)

Re: UnmanagedType.Boolとboolの違い

Posted: 2017年1月04日(水) 20:31
by ちゃん太
dllファイルのBOOL型とC#のbool型を互換してるって事でいいですか?

Re: UnmanagedType.Boolとboolの違い

Posted: 2017年1月04日(水) 20:40
by Math
そうです。dll はC/C++で作ったものでC#とは違うものです。それをMarshalASで”C#の形に変えている”ということです。VSのVersionによって表示が違うかもしれません。私はVS2015です。なにをお使いですか?

Re: UnmanagedType.Boolとboolの違い

Posted: 2017年1月04日(水) 21:05
by ちゃん太
Mathさん
ありがとうございます。ここ数日一番自分の理解度が高まったトピックでした。