Results 1 to 2 of 2

Thread: How do I extract an Icon from DLL-file

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    1
    I have a really serious problem. I was building my own explorer but when it came to adding icons I got stuck. You can retreive the file association from the registry, and in the registry it also tells you where the program Icon is located and an integer number. But I don't know how to use this information.
    Anyone help me please!

  2. #2
    Lively Member
    Join Date
    Jun 1999
    Location
    East Anglia, England
    Posts
    73
    Hello,
    Here we go, all the code you need,
    Place the following code into a module
    Code:
    Option Explicit
    
    Private Type PicBmp
       Size As Long
       tType As Long
       hBmp As Long
       hPal As Long
       Reserved As Long
    End Type
    
    Private Type GUID
       Data1 As Long
       Data2 As Integer
       Data3 As Integer
       Data4(7) As Byte
    End Type
    
    Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    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 DestroyIcon Lib "user32" (ByVal hicon As Long) As Long
    
    '---FUNCTION---'
    
    Public Function GetIconFromFile(FileName As String, IconIndex As Long, UseLargeIcon As Boolean) As Picture
        
        'Parameters:
        'FileName - File (EXE or DLL) containing icons
        'IconIndex - Index of icon to extract, starting with 0
        'UseLargeIcon-True for a large icon, False for a small icon
        'Returns: Picture object, containing icon
        
        Dim hlargeicon As Long
        Dim hsmallicon As Long
        Dim selhandle As Long
        
        'IPicture requires a reference to "Standard OLE Types."
        Dim pic As PicBmp
        Dim IPic As IPicture
        Dim IID_IDispatch As GUID
        
        If ExtractIconEx(FileName, IconIndex, hlargeicon, hsmallicon, 1) > 0 Then
            If UseLargeIcon Then
                selhandle = hlargeicon
            Else
                selhandle = hsmallicon
            End If
        
            'Fill in with IDispatch Interface ID.
            With IID_IDispatch
                .Data1 = &H20400
                .Data4(0) = &HC0
                .Data4(7) = &H46
            End With
            'Fill Pic with necessary parts.
            With pic
                .Size = Len(pic) 'Length of structure.
                .tType = vbPicTypeIcon 'Type of Picture (bitmap).
                .hBmp = selhandle 'Handle to bitmap.
            End With
            
            'Create Picture object.
            Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)
            
            'Return the new Picture object.
            Set GetIconFromFile = IPic
            
            DestroyIcon hsmallicon
            DestroyIcon hlargeicon
        
        End If
    End Function
    Add a command button and 2 picture boxes to a form and then paste this code in,
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        '//LARGE ICON
        Set Picture1.Picture = GetIconFromFile("c:\windows\moricons.dll", 0, True)
        '//SMALL ICON
        Set Picture2.Picture = GetIconFromFile("c:\windows\moricons.dll", 0, False)
    End Sub
    Hope it helps,
    Desire.

Posting Permissions

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



Click Here to Expand Forum to Full Width