i want to extract icon from exe file.
does anyone know how to do this??

thank you very much

Regards
Estella

Dani AI

Generated

Quick follow-up and extras to the SHGetFileInfo approach that solved 's question (thanks to for the working sample).

The SHGetFileInfo route is a standard, lightweight way to show an EXE/DLL icon from the system image list; note that SHGetFileInfo sometimes returns a system image-list handle and sometimes an HICON depending on flags, and any HICON returned must be released when no longer needed. SHGetFileInfo also expects COM to be initialized when called from nonstandard threads. (learn.microsoft.com)

Two common alternatives that give more control:

  • ExtractIconEx — enumerates and extracts large or small HICON handles by index (nIconIndex = -1 returns the number of icons inside the file). Extracted HICONs must be destroyed with DestroyIcon when finished. (learn.microsoft.com)
  • PrivateExtractIcons — exposes additional size/format options and is useful for extracting higher-resolution resources (Windows Vista+ icons may include 256x256 PNG-compressed images). (learn.microsoft.com)

Example (VB6): extract one icon and draw it into a PictureBox (different approach than the SHGetFileInfo/ImageList_Draw code already posted).

' Declarations
Private Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" _
    (ByVal lpszFile As String, ByVal nIconIndex As Long, phiconLarge As Long, phiconSmall As Long, ByVal nIcons As Long) As Long
Private Declare Function DrawIcon Lib "user32.dll" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Private Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Long) As Long

Public Sub DrawExeIcon(path As String, index As Long, pic As PictureBox, large As Boolean)
    Dim hIcon As Long
    If large Then
        ExtractIconEx path, index, hIcon, 0&, 1
    Else
        ExtractIconEx path, index, 0&, hIcon, 1
    End If
    pic.Cls
    Call DrawIcon(pic.hDC, 0, 0, hIcon)
    Call DestroyIcon(hIcon)
End Sub

Notes and troubleshooting

  • If an IPicture/IPictureDisp is required (for assigning to Picture properties), create one with OleCreatePictureIndirect and consider ownership flags so the picture object frees the handle when appropriate. (learn.microsoft.com)
  • Always match any API that returns an HICON with the correct cleanup (DestroyIcon or let OleCreatePictureIndirect own it). (learn.microsoft.com)
  • If the intent behind the later post from was unpacking files from a self-extracting installer (rather than icons), common nonprogrammatic tools are 7-Zip/Universal Extractor/Resource Hacker; programmatic extraction requires treating the EXE as an installer/archive or using the Win32 resource APIs (FindResource/LoadResource) to pull embedded resources.

Key references: SHGetFileInfo, ExtractIconEx, PrivateExtractIcons, DestroyIcon, OleCreatePictureIndirect. (learn.microsoft.com)

Recommended Answers

All 10 Replies

declared on module...

Private Const SHGFI_DISPLAYNAME = &H200, SHGFI_EXETYPE = &H2000, SHGFI_SYSICONINDEX = &H4000, SHGFI_LARGEICON = &H0, SHGFI_SMALLICON = &H1, SHGFI_SHELLICONSIZE = &H4, SHGFI_TYPENAME = &H400, ILD_TRANSPARENT = &H1, BASIC_SHGFI_FLAGS = SHGFI_TYPENAME Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE
Private Type SHFILEINFO
    hIcon As Long: iIcon As Long: dwAttributes As Long: szDisplayName As String * MAX_PATH: szTypeName As String * 80
End Type
Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function ImageList_Draw Lib "Comctl32.dll" (ByVal himl As Long, ByVal i As Long, ByVal hDCDest As Long, ByVal X As Long, ByVal Y As Long, ByVal Flags As Long) As Long
Private shinfo As SHFILEINFO, sshinfo As SHFILEINFO

Public Enum IconRetrieve
    ricnLarge = 32
    ricnSmall = 16
End Enum
commented: Great Code +1
commented: awesome +1

Add in your declaration :

Const MAX_PATH As Integer = 260
commented: :twisted: +3

thanks to completed my declaration.
actually i don't have idea to do this function.
do you know a function to retrieve icon from exe file using my delcaration?

add this following code in your module :

Public Sub RetrieveIcon(fName As String, DC As PictureBox, icnSize As IconRetrieve)
    Dim hImgSmall, hImgLarge As Long  'the handle to the system image list
    DC.Cls
    Select Case icnSize
    Case ricnSmall
        hImgSmall = SHGetFileInfo(fName$, 0&, shinfo, Len(shinfo), BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
        Call ImageList_Draw(hImgSmall, shinfo.iIcon, DC.hDC, 0, 0, ILD_TRANSPARENT)
    Case ricnLarge
        hImgLarge& = SHGetFileInfo(fName$, 0&, shinfo, Len(shinfo), BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON)
        Call ImageList_Draw(hImgLarge, shinfo.iIcon, DC.hDC, 0, 0, ILD_TRANSPARENT)
    End Select
End Sub

this function will called like this :

RetrieveIcon lblPath.Caption, PicIcon32, ricnLarge

PicIcon32 is picturebox

hope this helps...

commented: Really great code. you're the great coder with awesome codes. thank you very much Jx_Man :) +1
commented: never seen before +1
commented: Nice +1
commented: Always Give the best answer...Thanks jx_man +1

yes, got it.
Working like a charm.

RetrieveIcon "C:\Program Files\Winamp\winamp.exe", PicIcon32, ricnLarge

Thank you very much

btw if i want to see the small one, i use bellow code :

RetrieveIcon "C:\Program Files\Winamp\winamp.exe", PicIcon32, ricnSmall

That right jx??

yes, thats right...

commented: many thanks friend :) +1

Ok. Thanks again.
you really help me much with great code.
This thread already solved.

you're Welcome...
Happy coding Friend :)

hello every one,

how to extract .exe files using vb 6.0. any one pls help me out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.