正确使用ExtractIconEx nIconIndex参数?

 手机用户2502931303 发布于 2023-01-02 15:56

我试图以编程方式从注册表中提取一些图标.但是我注意到不一致的行为.我在这里做了一个基本测试:

https://gist.github.com/CoenraadS/86e80d8e7279b64b7989

class Program
{
    [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);

    static void Main(string[] args)
    {
        IntPtr largeIconPtr = IntPtr.Zero;
        IntPtr smallIconPtr = IntPtr.Zero;

        //HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{1206F5F1-0569-412C-8FEC-3204630DFB70}\DefaultIcon
        Console.WriteLine("Vault.dll");
        ExtractIconEx(@"%SystemRoot%\system32\Vault.dll", 1, out largeIconPtr, out smallIconPtr, 1);
        Console.WriteLine("Icon Index = 1");
        Console.WriteLine("Large: " + largeIconPtr.ToString());
        Console.WriteLine("Small: " + smallIconPtr.ToString());
        Console.WriteLine();

        Console.WriteLine("Icon Index = -1");
        ExtractIconEx(@"%SystemRoot%\system32\Vault.dll", -1, out largeIconPtr, out smallIconPtr, 1);            
        Console.WriteLine("Large: " + largeIconPtr.ToString());
        Console.WriteLine("Small: " + smallIconPtr.ToString());
        Console.WriteLine();

        //HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{40419485-C444-4567-851A-2DD7BFA1684D}\DefaultIcon
        Console.WriteLine("telephon.cpl");
        Console.WriteLine("Icon Index = 100");
        ExtractIconEx(@"%SystemRoot%\System32\telephon.cpl", 100, out largeIconPtr, out smallIconPtr, 1);
        Console.WriteLine("Large: " + largeIconPtr.ToString());
        Console.WriteLine("Small: " + smallIconPtr.ToString());
        Console.WriteLine();

        Console.WriteLine("Icon Index = -100");
        ExtractIconEx(@"%SystemRoot%\System32\telephon.cpl", -100, out largeIconPtr, out smallIconPtr, 1);
        Console.WriteLine("Large: " + largeIconPtr.ToString());
        Console.WriteLine("Small: " + smallIconPtr.ToString());
        Console.ReadLine();
    }
}

如果我阅读MSDN文章了解索引的工作原理:

如果此值为负数且phiconLarge或phiconSmall不为NULL,则该函数首先提取其资源标识符等于nIconIndex的绝对值的图标.例如,使用-3来提取资源标识符为3的图标.

但是我不能在我的结果中复制这个.我可以通过检查结果是否为0来解决它,然后翻转值并再次运行它,但我觉得必须有一个更好的解决方案.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有