Search code examples
c#shell32.dll

How can I use the images within shell32.dll in my C# project?


How can I use the images within shell32.dll in my C# project?


Solution

  • You can extract icons from a DLL with this code:

    public class IconExtractor
    {
    
        public static Icon Extract(string file, int number, bool largeIcon)
        {
            IntPtr large;
            IntPtr small;
            ExtractIconEx(file, number, out large, out small, 1);
            try
            {
                return Icon.FromHandle(largeIcon ? large : 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);
    
    }
    
    ...
    
    form.Icon = IconExtractor.Extract("shell32.dll", 42, true);
    

    Of course you need to know the index of the image in the DLL...