none
How do i Extract an icon from a dll (ExtractIcon - Shell32.dll) RRS feed

  • Question

  • Ive traced the default file icon back to the dll where the images are stored (small and large), however, how do i read the dll to get the inside images?

    An example dll is: C:\Windows\system32\imageres.dll  (notepad, has an id of 102, dunno if that is relevent)

    Ive been looking at ExtractIcon (in Shell32.dll), but i dont know how to use it (all forum posts are on visual basic):

    http://msdn.microsoft.com/en-us/library/ms648068(v=vs.85).aspx

    Saturday, January 1, 2011 9:07 AM

Answers

  •  

    Hi Fatal Berserker,

     

    Welcome to MSDN Forums!

     

    We can use the following sample code to extract the icon from the dll file.

     

    And you will need to add the "/unsafe" option when you compile this code file.

     

     

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication1
    {
      class ExtractIconExample
      {
        /* CONSTRUCTORS */
        static ExtractIconExample()
        {
        }
    
        // HIDE INSTANCE CONSTRUCTOR
        private ExtractIconExample()
        {
        }
    
        [DllImport("Shell32", CharSet = CharSet.Auto)]
        private static unsafe extern int ExtractIconEx(
          string lpszFile,
          int nIconIndex,
          IntPtr[] phIconLarge,
          IntPtr[] phIconSmall,
          int nIcons);
    
        [DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)]
        private static unsafe extern int DestroyIcon(IntPtr hIcon);
    
        public static Icon ExtractIconFromExe(string file, bool large)
        {
          unsafe
          {
            int readIconCount = 0;
            IntPtr[] hDummy = new IntPtr[1] { IntPtr.Zero };
            IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero };
    
            try
            {
              if (large)
                readIconCount = ExtractIconEx(file, 0, hIconEx, hDummy, 1);
              else
                readIconCount = ExtractIconEx(file, 0, hDummy, hIconEx, 1);
    
              if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero)
              {
                // GET FIRST EXTRACTED ICON
                Icon extractedIcon = (Icon)Icon.FromHandle(hIconEx[0]).Clone();
    
                return extractedIcon;
              }
              else // NO ICONS READ
                return null;
            }
            catch (Exception ex)
            {
              /* EXTRACT ICON ERROR */
    
              // BUBBLE UP
              throw new ApplicationException("Could not extract icon", ex);
            }
            finally
            {
              // RELEASE RESOURCES
              foreach (IntPtr ptr in hIconEx)
                if (ptr != IntPtr.Zero)
                  DestroyIcon(ptr);
    
              foreach (IntPtr ptr in hDummy)
                if (ptr != IntPtr.Zero)
                  DestroyIcon(ptr);
            }
          }
        }
      }
    }
    
    

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 4, 2011 8:37 AM
    Moderator
  • So different is the VB code in this thread from C#

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/ed0ec792-911b-4ed0-ac63-e24c1f95f95b

    Free snippet converter

    http://www.tangiblesoftwaresolutions.com/


    Success
    Cor
    Saturday, January 1, 2011 9:54 AM

All replies

  • So different is the VB code in this thread from C#

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/ed0ec792-911b-4ed0-ac63-e24c1f95f95b

    Free snippet converter

    http://www.tangiblesoftwaresolutions.com/


    Success
    Cor
    Saturday, January 1, 2011 9:54 AM
  •  

    Hi Fatal Berserker,

     

    Welcome to MSDN Forums!

     

    We can use the following sample code to extract the icon from the dll file.

     

    And you will need to add the "/unsafe" option when you compile this code file.

     

     

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication1
    {
      class ExtractIconExample
      {
        /* CONSTRUCTORS */
        static ExtractIconExample()
        {
        }
    
        // HIDE INSTANCE CONSTRUCTOR
        private ExtractIconExample()
        {
        }
    
        [DllImport("Shell32", CharSet = CharSet.Auto)]
        private static unsafe extern int ExtractIconEx(
          string lpszFile,
          int nIconIndex,
          IntPtr[] phIconLarge,
          IntPtr[] phIconSmall,
          int nIcons);
    
        [DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)]
        private static unsafe extern int DestroyIcon(IntPtr hIcon);
    
        public static Icon ExtractIconFromExe(string file, bool large)
        {
          unsafe
          {
            int readIconCount = 0;
            IntPtr[] hDummy = new IntPtr[1] { IntPtr.Zero };
            IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero };
    
            try
            {
              if (large)
                readIconCount = ExtractIconEx(file, 0, hIconEx, hDummy, 1);
              else
                readIconCount = ExtractIconEx(file, 0, hDummy, hIconEx, 1);
    
              if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero)
              {
                // GET FIRST EXTRACTED ICON
                Icon extractedIcon = (Icon)Icon.FromHandle(hIconEx[0]).Clone();
    
                return extractedIcon;
              }
              else // NO ICONS READ
                return null;
            }
            catch (Exception ex)
            {
              /* EXTRACT ICON ERROR */
    
              // BUBBLE UP
              throw new ApplicationException("Could not extract icon", ex);
            }
            finally
            {
              // RELEASE RESOURCES
              foreach (IntPtr ptr in hIconEx)
                if (ptr != IntPtr.Zero)
                  DestroyIcon(ptr);
    
              foreach (IntPtr ptr in hDummy)
                if (ptr != IntPtr.Zero)
                  DestroyIcon(ptr);
            }
          }
        }
      }
    }
    
    

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 4, 2011 8:37 AM
    Moderator