CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2002
    Posts
    72

    Cool Icon path from registry

    I want to attach the icon of the file name in a listview item at runtime.So , i want to search the path for the icon for the particular extension in the system registry. I want the appropiate function for that.

    Plz help me out soon
    thnx

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    Copied from a previous post:

    Option Explicit
    Const REG_SZ = 1 ' Unicode nul terminated string
    Const REG_BINARY = 3 ' Free form binary
    Const REG_NUM = 4 'Had to add this one...???
    Const HKEY_CURRENT_USER = &H80000001
    Const HKEY_CLASSES_ROOT = &H80000000
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

    Private Function GetIconPath(strExt As String) As String
    Dim strDesc As String
    Dim strIcon As String

    strDesc = GetString(HKEY_CLASSES_ROOT, strExt, "")
    strIcon = GetString(HKEY_CLASSES_ROOT, strDesc & "\DefaultIcon", "")

    GetIconPath = strIcon

    End Function

    Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
    Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
    'retrieve nformation about the key
    lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
    If lResult = 0 Then
    If lValueType = REG_SZ Then
    'Create a buffer
    strBuf = String(lDataBufSize, Chr$(0))
    'retrieve the key's content
    lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
    If lResult = 0 Then
    'Remove the unnecessary chr$(0)'s
    RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
    End If
    ElseIf lValueType = REG_BINARY Or lValueType = REG_NUM Then
    Dim strData As Integer
    'retrieve the key's value
    lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
    If lResult = 0 Then
    RegQueryStringValue = strData
    End If
    End If
    End If
    End Function

    Function GetString(hKey As Long, strPath As String, strValue As String)
    Dim ret
    'Open the key
    RegOpenKey hKey, strPath, ret
    'Get the key's content
    GetString = RegQueryStringValue(ret, strValue)
    'Close the key
    RegCloseKey ret
    End Function

    Private Sub Command1_Click()
    MsgBox GetIconPath(Text1.Text)
    End Sub


    Your next task will be to figure out how to call ExtractIcon...

  3. #3
    Join Date
    Aug 2000
    Location
    South East England
    Posts
    86

    Re: Icon path from registry

    So a .txt file is txtfile
    a .htm or .html file is htmlfile

    HKEY_CLASSES_ROOT\htmlfile\DefaultIcon = "%1"
    HKEY_CLASSES_ROOT\txtfile\DefaultIcon = %SystemRoot%\system32\shell32.dll,-152

    So what file is "%1" and as there is no icon index, is it a default icon index 0?

    File %SystemRoot%\system32\shell32.dll
    = C:\Windows\system32\shell32.dll
    but -152 can't be the icon index as it is less than zero

    Icon 152 in file shell32.dll is a CD-Rom image.
    The text (.txt) icon is icon 70.

    I too am trying to get icon path and icon index from a files extension.
    Please help.

    I've attached a program that displayes all icons in a dll or exe file.
    (Don't use the save option as it is only partly working)
    Attached Files Attached Files
    Don't spend too much time on your computer.
    Sit on a chair instead!!
    It's a lot more comfortable and better for your hardware.

    :-/

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

Featured