CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Problem with FindResource

  1. #1
    Join Date
    Aug 2010

    Unhappy Problem with FindResource

    So, here's my problem. After two long days of hard-searching I couldn't find anything to help me on this. Basically I want to extract the default icon of an executable file.

    I tried to use ExtractIcon function, but after I get the handle to an icon I couldn't find any way to get a pointer to that icon. I want to use that icon, not just show it using DrawIcon, so, I think ExtractIcon doesn't help me too much.

    The next thing I've tried is to use FindResource, and it does seem to work when I try to load a custom resource added by me in a simple VB application, but when I'm trying to load some resources added with UpdateResource function it wouldn't find them. I am not even sure what I should use as a second and third parameter. On MSDN appears a MAKEINTRESOURCE function which exists only in C.

    Initially I thought there is something wrong with my code, but then I've tried EnumResourceNames too (which wouldn't be a great solution to just extract one icon), and it doesn't return anything, except when I try it on that VB application I've mentioned before, and it returns a single result.

    Btw, when I open that app with ResHack I can see all resources, including those I've added with UpdateResource, and also icons and stuff.

    I am looking for a short code so I can understand it, nothing too fancy!
    Thanks in advance for your responses.

  2. #2
    Join Date
    Jul 2001
    Sunny South Africa

    Re: Problem with FindResource

    With this, you can find any file's icon :

    Option Explicit
    Const MAX_PATH As Integer = 260
    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
    Private sshinfo As SHFILEINFO
    Public Enum IconSize
        IconLarge = 32
        IconSmall = 16
    End Enum
    Public Sub GetFileIcon(FileName As String, DeviceContext As PictureBox, icnSize As IconSize)
        Dim hImgSmall, hImgLarge As Long  'the handle to the system image list
        Select Case icnSize
        Case IconSmall
            hImgSmall = SHGetFileInfo(FileName, 0&, shinfo, Len(shinfo), BASIC_SHGFI_FLAGS Or SHGFI_SMALLICON)
            Call ImageList_Draw(hImgSmall, shinfo.iIcon, DeviceContext.hDC, 0, 0, ILD_TRANSPARENT)
        Case IconLarge
            hImgLarge& = SHGetFileInfo(FileName, 0&, shinfo, Len(shinfo), BASIC_SHGFI_FLAGS Or SHGFI_LARGEICON)
            Call ImageList_Draw(hImgLarge, shinfo.iIcon, DeviceContext.hDC, 0, 0, ILD_TRANSPARENT)
        End Select
    End Sub
    Private Sub Command1_Click()
    GetFileIcon Text1.Text, Picture1, IconLarge
    End Sub
    I just had to include it!

    I think you may need the GetIconInfo API, but what exactly do you want to do with the icon?

    This may also help :


    Last edited by HanneSThEGreaT; August 24th, 2010 at 06:27 AM.
    All my Articles

  3. #3
    Join Date
    Aug 2010

    Re: Problem with FindResource

    Hannes, I took a look at your code, but like ExtractIcon() it returns only a handle to that icon. Is there a way to transform that handle into a pointer to icon's byte array? I know there is LoadResource() function, but only works with FindResource().

    I want to use this icon with UpdateResource() to set it to another exe.
    Last edited by bluepoint; August 28th, 2010 at 10:36 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)