HomeC#技術項目 exe、dllファイルからアイコン抽出

exe、dllファイルからアイコン抽出

宣言

// ExtractIconEx関数
[DllImport("shell32.dll", EntryPoint = "ExtractIconEx", CharSet = CharSet.Auto)]
public static extern int ExtractIconEx(
    [MarshalAs(UnmanagedType.LPTStr)] string file,
    int index,
    out IntPtr largeIconHandle,
    out IntPtr smallIconHandle,
    int icons
);

[DllImport("User32.dll")]
private static extern bool DestroyIcon(IntPtr hIcon);

アイコンを抽出する

//「dll名」と「アイコン番号」からアイコンを取得する
// IconSize
// true :大きいアイコン
// false:小さいアイコン
private Icon GetIconFromEXEDLL(string path, int iconIndex, bool iconSize)
{
    try
    {
        Icon[] icons = new Icon[2];
        IntPtr largeIconHandle = IntPtr.Zero;
        IntPtr smallIconHandle = IntPtr.Zero;
        ExtractIconEx(path, iconIndex, out largeIconHandle, out smallIconHandle, 1);
        icons[0] = (Icon)Icon.FromHandle(largeIconHandle).Clone();
        icons[1] = (Icon)Icon.FromHandle(smallIconHandle).Clone();
        DestroyIcon(largeIconHandle);
        DestroyIcon(smallIconHandle);

        if (iconSize)
        {
            return icons[0];
        }
        else
        {
            return icons[1];
        }
    }
    catch (Exception){ }
}



ページトップへ

データベース

サーバ

Copyright (C) MadCap. All Rights Reserved.