UnmanagedType.Boolとboolの違い

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ちゃん太

UnmanagedType.Boolとboolの違い

#1

投稿記事 by ちゃん太 » 8年前

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

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

Math

Re: UnmanagedType.Boolとboolの違い

#2

投稿記事 by Math » 8年前

これはマーシャリングといって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の違い

#3

投稿記事 by ちゃん太 » 8年前

Mathさんが書かれたコードの場合、てかあるサイトのコードで僕もそれを見ていましたけど、その場合MarshalAS~を書かないとエラーになるんですか?

Math

Re: UnmanagedType.Boolとboolの違い

#4

投稿記事 by Math » 8年前

もちろんです。実際にやってみると”エラー 1 ID がありません。”となります。VisualStudioは2015を使っていますか?(私はVS2015です)

ちゃん太

Re: UnmanagedType.Boolとboolの違い

#5

投稿記事 by ちゃん太 » 8年前

dllファイルのBOOL型とC#のbool型を互換してるって事でいいですか?

Math

Re: UnmanagedType.Boolとboolの違い

#6

投稿記事 by Math » 8年前

そうです。dll はC/C++で作ったものでC#とは違うものです。それをMarshalASで”C#の形に変えている”ということです。VSのVersionによって表示が違うかもしれません。私はVS2015です。なにをお使いですか?

ちゃん太

Re: UnmanagedType.Boolとboolの違い

#7

投稿記事 by ちゃん太 » 8年前

Mathさん
ありがとうございます。ここ数日一番自分の理解度が高まったトピックでした。

閉鎖

“C言語何でも質問掲示板” へ戻る