我试图以编程方式从注册表中提取一些图标.但是我注意到不一致的行为.我在这里做了一个基本测试:
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来解决它,然后翻转值并再次运行它,但我觉得必须有一个更好的解决方案.