从注册表中找到的dll中检索图标

本文关键字:dll 检索 图标 注册表 | 更新日期: 2023-09-27 18:05:17

我目前正试图得到一个windows资源管理器像程序启动和运行(通过teleerik)。为此,我使用了两个步骤来获取图标:

  1. Icon.ExtractAssociatedIcon (fileNameAndPath) .ToBitmap ();
  2. 如果1;一个失败了,我在注册表中使用图标作为扩展的默认图标。

到目前为止,我在搜索的文件中找到图标,我从注册表中提取"C:'WINDOWS'system32'imageres.dll,-102"。

我现在已经看到了一些解决方案,说你必须使用SHGetFileInfo或第三方工具,... .我不确定虽然如何SHGetFileInfo是在这种情况下使用(尝试了几次现在无济于事)。

所以我的问题是,我必须把我的尝试到目前为止的参数当我试图处理图标时,只带来了异常。

Telerik.WinControls.NativeMethods.SHFILEINFO shFileInfo = new NativeMethods.SHFILEINFO();
Telerik.WinControls.NativeMethods.SHGetFileInfo(fileNameAndPath, Telerik.WinControls.NativeMethods.SHGFI_SMALLICON, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), Telerik.WinControls.NativeMethods.SHGFI_SMALLICON);
Image returnImage = System.Drawing.Icon.FromHandle(shFileInfo.hIcon).ToBitmap();

*也作为信息当可能的时候,我想使用:telererik . wincontrols . native emethods . shgetfileinfo(从我所看到的似乎没有太大的区别,该方法的非telererik版本)。

作为一个注意:即使我发现如何做到这一点与ExtractIconEx(如何做到这张贴作为答案),我仍然宁愿使用SHFileInfo如果可能的话,因为这意味着我不需要包括一个外部dll)*

从注册表中找到的dll中检索图标

一个工作的变体将使用ExtractIconEx而不是SHFileInfo(尽管我更喜欢SHFileInfo)。

如果我在字符串变量keyValue中拥有注册表中的键值(因此"C: ' WINDOWS ' system32系统' imageres.dll -102"):

string[] splitKeyValue = keyValue.ToString().Split(',');
IntPtr iconLarge = new IntPtr();
IntPtr iconSmall = new IntPtr();
ExtractIconEx(splitKeyValue[0], Convert.ToInt32(splitKeyValue[1]), ref iconLarge, ref iconSmall, 1);
Image returnImage = Icon.FromHandle(iconLarge).ToBitmap();

这将使我得到一个大图标。

在基类的某个地方,我还需要放入以下行:

     [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    internal static extern int ExtractIconEx(string stExeFileName, int nIconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, int nIcons);