从 C# 中的 .dll 文件中提取图标组

Extracting an icon group from a .dll file in C#

我正在尝试从 imageres.dll 中提取一个图标。特别是 "My Computer" 或 "This PC" 图标。问题是在 Win7 和 Win10 之间,图标编号发生变化。但是,图标组没有 (109)。有没有办法获取那个图标组,然后让计算机找出该组中使用哪个图标,就像它找出我的应用程序使用哪个图标一样?

这是我用来通过索引获取特定图标的代码:

public class GetIcon {
    public static Icon Extract(string file, int number) {
        IntPtr large;
        IntPtr small;
        ExtractIconEx(file, number, out large, out small, 1);
        try {
            return Icon.FromHandle(small);
        }
        catch {
            return null;
        }
    }
    [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);
}

谢谢。

有几种方法可以做到这一点。最可靠但也可能最耗时(前提是您找不到现有库)的方法是解析 PE 文件(即 .exe、.dll)并自行提取相关的图标组数据。这里有一个很好的格式资源:https://msdn.microsoft.com/en-us/library/ms809762.aspx

第二种方法,使用 Windows 函数可以很容易地完成,但是有一个警告。它仅适用于与您的应用程序具有相同位类型的 PE 文件。因此,例如,如果您的应用程序是 64 位的,则它只能在 64 位 PE 文件上运行。

这是我刚刚编写的函数 - 基于此:https://msdn.microsoft.com/en-us/library/windows/desktop/ms648051(v=vs.85).aspx#_win32_Sharing_Icon_Resources,它接受文件名、组号和所需的图标大小,以及 returns System.Drawing.Icon

[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);
[DllImport("kernel32.dll")]
static extern IntPtr FindResource(IntPtr hModule, int lpName, int lpType);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo);
[DllImport("kernel32.dll")]
static extern IntPtr LockResource(IntPtr hResData);
[DllImport("user32.dll")]
static extern int LookupIconIdFromDirectoryEx(byte[] presbits, bool fIcon, int cxDesired, int cyDesired, uint Flags);
[DllImport("user32.dll")]
static extern IntPtr CreateIconFromResourceEx(byte[] pbIconBits, uint cbIconBits, bool fIcon, uint dwVersion, int cxDesired, int cyDesired, uint uFlags);
[DllImport("kernel32.dll", SetLastError = true)]
static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo);

const int RT_GROUP_ICON = 14;
const int RT_ICON = 0x00000003;

private System.Drawing.Icon GetIconFromGroup(string file, int groupId, int size)
{
    IntPtr hExe = LoadLibrary(file);
    if(hExe != IntPtr.Zero)
    {

        IntPtr hResource = FindResource(hExe, groupId, RT_GROUP_ICON);

        IntPtr hMem = LoadResource(hExe, hResource);

        IntPtr lpResourcePtr = LockResource(hMem);
        uint sz = SizeofResource(hExe, hResource);
        byte[] lpResource = new byte[sz];
        Marshal.Copy(lpResourcePtr, lpResource, 0, (int)sz);

        int nID = LookupIconIdFromDirectoryEx(lpResource, true, size, size, 0x0000);

        hResource = FindResource(hExe, nID, RT_ICON);

        hMem = LoadResource(hExe, hResource);

        lpResourcePtr = LockResource(hMem);
        sz = SizeofResource(hExe, hResource);
        lpResource = new byte[sz];
        Marshal.Copy(lpResourcePtr, lpResource, 0, (int)sz);

        IntPtr hIcon = CreateIconFromResourceEx(lpResource, sz, true, 0x00030000, size, size, 0);

        System.Drawing.Icon testIco = System.Drawing.Icon.FromHandle(hIcon);

        return testIco;
    }

    return null;
}

流程基本上是这样的:

  1. 使用LoadLibrary加载.exe或.dll文件
  2. 获取 RT_GROUP_ICON 资源的句柄和数据
  3. 将数据连同所需的大小一起传递给 LookupIconIdFromDirectoryEx,以获取图标索引
  4. 从那里,您可以使用 ExtractIconEx,或者只使用图标索引重复步骤 2,然后使用 RT_ICON,然后使用 CreateIconFromResourceEx 获取您的图标句柄。