Results 1 to 3 of 3

Thread: [Resolved] Retrieving Icon From Program via Shell

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    [Resolved] Retrieving Icon From Program via Shell

    Is it possible to retirieve the icon from a program using the shell function?

    I don't know if i'm asking this qusetion correctly so i'll show you some code...


    CreateEntryToSystemPanel "{9d6D8ED6-116D-4D4E-B1C2-87098DB509BA}", "My Program", "My Program Tooltip", "C:\MyProgram.exe,0", "C:\MyProgram.exe -options"

    The bold bit is where the icon path should go.

    I saw that someone was using the ,0 as at the end of the program path and i assumed that that would extract the icon. However it's not working. It works fine if i use a *.ico file, but i want this program to only have 1 file, so i was wondering if it's possible to put the icon there! There is an icon now, but it's the Visual Basic default icon and not my program's icon (in project propeties).

    Thanks for your time.

    *** EDIT:
    Don't worry, it started working now, i don't know how it fixed it self. The way i posted above works fine.
    Last edited by Slyke; Jul 29th, 2006 at 09:19 AM.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Retrieving Icon From Program via Shell

    Can you post the code for CreateEntryToSystemPanel? As I understand it, that writes information to the registry so your program is added to the control panel applet window. But without knowing where or how it writes the different values I can't really tell you if the syntax you're using is correct.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Retrieving Icon From Program via Shell

    Sure thing

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function RegCloseKey Lib "advapi32" ( _
    4.   ByVal hKey As Long) As Long
    5.  
    6. Private Declare Function RegCreateKeyEx Lib "advapi32" _
    7.   Alias "RegCreateKeyExA" ( _
    8.   ByVal hKey As Long, _
    9.   ByVal lpSubKey As String, _
    10.   ByVal Reserved As Long, _
    11.   ByVal lpClass As String, _
    12.   ByVal dwOptions As Long, _
    13.   ByVal samDesired As Long, _
    14.   ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    15.   ByRef phkResult As Long, _
    16.   ByRef lpdwDisposition As Long) As Long
    17.  
    18. Private Declare Function RegOpenKeyEx Lib "advapi32" _
    19.   Alias "RegOpenKeyExA" ( _
    20.   ByVal hKey As Long, _
    21.   ByVal lpSubKey As String, _
    22.   ByVal ulOptions As Long, _
    23.   ByVal samDesired As Long, _
    24.   ByRef phkResult As Long) As Long
    25.  
    26. Private Declare Function RegQueryValueEx Lib "advapi32" _
    27.   Alias "RegQueryValueExA" ( _
    28.   ByVal hKey As Long, _
    29.   ByVal lpValueName As String, _
    30.   ByVal lpReserved As Long, _
    31.   ByRef lpType As Long, _
    32.   ByVal lpData As String, _
    33.   ByRef lpcbData As Long) As Long
    34.  
    35. Private Declare Function RegSetValueEx Lib "advapi32" _
    36.   Alias "RegSetValueExA" ( _
    37.   ByVal hKey As Long, _
    38.   ByVal lpValueName As String, _
    39.   ByVal Reserved As Long, _
    40.   ByVal dwType As Long, _
    41.   ByVal lpData As String, _
    42.   ByVal cbData As Long) As Long
    43.  
    44. Private Declare Function RegSetValueExB Lib "advapi32.dll" _
    45.   Alias "RegSetValueExA" ( _
    46.   ByVal hKey As Long, _
    47.   ByVal lpValueName As String, _
    48.   ByVal Reserved As Long, _
    49.   ByVal dwType As Long, _
    50.   ByRef lpData As Byte, _
    51.   ByVal cbData As Long) As Long
    52.  
    53. Private Declare Function RegDeleteKey Lib "advapi32.dll" _
    54.   Alias "RegDeleteKeyA" ( _
    55.   ByVal hKey As Long, _
    56.   ByVal lpSubKey As String) As Long
    57.  
    58. Private Declare Function RegCreateKey Lib "advapi32.dll" _
    59.   Alias "RegCreateKeyA" ( _
    60.   ByVal hKey As Long, _
    61.   ByVal lpSubKey As String, _
    62.   phkResult As Long) As Long
    63.  
    64.  
    65. Const REG_SZ = 1
    66. Const REG_EXPAND_SZ = 2
    67. Const REG_BINARY = 3&
    68. Const REG_DWORD = 4
    69.  
    70.  
    71. Const REG_OPTION_NON_VOLATILE = 0
    72.  
    73. Const READ_CONTROL = &H20000
    74. Const KEY_QUERY_VALUE = &H1
    75. Const KEY_SET_VALUE = &H2
    76. Const KEY_CREATE_SUB_KEY = &H4
    77. Const KEY_ENUMERATE_SUB_KEYS = &H8
    78. Const KEY_NOTIFY = &H10
    79. Const KEY_CREATE_LINK = &H20
    80. Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
    81. Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
    82. Const KEY_EXECUTE = KEY_READ
    83. Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
    84.   KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
    85.   KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
    86.  
    87.  
    88. Const HKEY_CLASSES_ROOT = &H80000000
    89. Const HKEY_CURRENT_USER = &H80000001
    90. Const HKEY_LOCAL_MACHINE = &H80000002
    91. Const HKEY_USERS = &H80000003
    92. Const HKEY_PERFORMANCE_DATA = &H80000004
    93.  
    94.  
    95. Const ERROR_NONE = 0
    96. Const ERROR_BADKEY = 2
    97. Const ERROR_ACCESS_DENIED = 8
    98. Const ERROR_SUCCESS = 0
    99.  
    100. Private Type SECURITY_ATTRIBUTES
    101.   nLength As Long
    102.   lpSecurityDescriptor As Long
    103.   bInheritHandle As Boolean
    104. End Type
    105.  
    106. Dim hKey As Long, MainKeyHandle As Long
    107. Dim rtn As Long, lBuffer As Long, sBuffer As String
    108. Dim lBufferSize As Long
    109. Dim lDataSize As Long
    110. Dim ByteArray() As Byte
    111.  
    112.  
    113. Private Function UpdateKey(KeyRoot As Long, _
    114.   KeyName As String, _
    115.   SubKeyName As String, _
    116.   SubKeyValue As String) As Boolean
    117.  
    118.   Dim rc As Long
    119.   Dim hKey As Long
    120.   Dim hDepth As Long
    121.   Dim lpAttr As SECURITY_ATTRIBUTES
    122.  
    123.   lpAttr.nLength = 50
    124.   lpAttr.lpSecurityDescriptor = 0
    125.   lpAttr.bInheritHandle = True
    126.  
    127.  
    128.   rc = RegCreateKeyEx(KeyRoot, KeyName, 0, REG_SZ, _
    129.     REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
    130.     hKey, hDepth)
    131.   If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError
    132.  
    133.   If (SubKeyValue = "") Then
    134.    
    135.     SubKeyValue = " "
    136.   End If
    137.  
    138.  
    139.   rc = RegSetValueEx(hKey, SubKeyName, 0, REG_SZ, _
    140.     SubKeyValue, LenB(StrConv(SubKeyValue, vbFromUnicode)))
    141.   If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError
    142.  
    143.  
    144.   rc = RegCloseKey(hKey)
    145.  
    146.  
    147.   UpdateKey = True
    148.   Exit Function
    149.  
    150. CreateKeyError:
    151.  
    152.   UpdateKey = False
    153.  
    154.   rc = RegCloseKey(hKey)
    155. End Function
    156.  
    157. Private Function CreateKey(SubKey As String)
    158.   Call ParseKey(SubKey, MainKeyHandle)
    159.   If MainKeyHandle Then
    160.     rtn = RegCreateKey(MainKeyHandle, SubKey, hKey)
    161.     If rtn = ERROR_SUCCESS Then
    162.       rtn = RegCloseKey(hKey)
    163.     End If
    164.   End If
    165. End Function
    166.  
    167. Private Function DeleteKey(KeyName As String)
    168.   Call ParseKey(KeyName, MainKeyHandle)
    169.   If MainKeyHandle Then
    170.     rtn = RegDeleteKey(MainKeyHandle, KeyName)
    171.   End If
    172. End Function
    173.  
    174. Private Function ErrorMsg(lErrorCode As Long) As String
    175.   Select Case lErrorCode
    176.     Case 1009, 1015
    177.       ErrorMsg = "The Registry Database is corrupt!"
    178.     Case 2, 1010
    179.       ErrorMsg = "Bad Key Name"
    180.     Case 1011
    181.       ErrorMsg = "Can't Open Key"
    182.     Case 4, 1012
    183.       ErrorMsg = "Can't Read Key"
    184.     Case 5
    185.       ErrorMsg = "Access to this key is denied"
    186.     Case 1013
    187.       ErrorMsg = "Can't Write Key"
    188.     Case 8, 14
    189.       ErrorMsg = "Out of memory"
    190.     Case 87
    191.       ErrorMsg = "Invalid Parameter"
    192.     Case 234
    193.       ErrorMsg = "There is more data than the buffer has been allocated to hold."
    194.     Case Else
    195.       ErrorMsg = "Undefined Error Code: " & Str$(lErrorCode)
    196.   End Select
    197. End Function
    198.  
    199. Private Function GetMainKeyHandle(MainKeyName As String) As Long
    200.   Const HKEY_CLASSES_ROOT = &H80000000
    201.   Const HKEY_CURRENT_USER = &H80000001
    202.   Const HKEY_LOCAL_MACHINE = &H80000002
    203.   Const HKEY_USERS = &H80000003
    204.   Const HKEY_PERFORMANCE_DATA = &H80000004
    205.   Const HKEY_CURRENT_CONFIG = &H80000005
    206.   Const HKEY_DYN_DATA = &H80000006
    207.  
    208.   Select Case MainKeyName
    209.     Case "HKEY_CLASSES_ROOT"
    210.       GetMainKeyHandle = HKEY_CLASSES_ROOT
    211.     Case "HKEY_CURRENT_USER"
    212.       GetMainKeyHandle = HKEY_CURRENT_USER
    213.     Case "HKEY_LOCAL_MACHINE"
    214.       GetMainKeyHandle = HKEY_LOCAL_MACHINE
    215.     Case "HKEY_USERS"
    216.       GetMainKeyHandle = HKEY_USERS
    217.     Case "HKEY_PERFORMANCE_DATA"
    218.       GetMainKeyHandle = HKEY_PERFORMANCE_DATA
    219.     Case "HKEY_CURRENT_CONFIG"
    220.       GetMainKeyHandle = HKEY_CURRENT_CONFIG
    221.     Case "HKEY_DYN_DATA"
    222.       GetMainKeyHandle = HKEY_DYN_DATA
    223.   End Select
    224. End Function
    225.  
    226. Private Sub ParseKey(KeyName As String, Keyhandle As Long)
    227.   rtn = InStr(KeyName, "\")
    228.   If Left(KeyName, 5) <> "HKEY_" Or Right(KeyName, 1) = "\" Then
    229.     MsgBox "Incorrect Format:" + Chr(10) + Chr(10) + KeyName
    230.     Exit Sub
    231.   ElseIf rtn = 0 Then
    232.     Keyhandle = GetMainKeyHandle(KeyName)
    233.     KeyName = ""
    234.   Else
    235.     Keyhandle = GetMainKeyHandle(Left(KeyName, rtn - 1))
    236.     KeyName = Right(KeyName, Len(KeyName) - rtn)
    237.   End If
    238. End Sub
    239.  
    240. Private Function SetBinaryValue(SubKey As String, Entry As String, _
    241.   Value As String, Optional ByVal DisplayErrorMsg As Boolean = True)
    242.  
    243.   Dim i As Long
    244.  
    245.   Call ParseKey(SubKey, MainKeyHandle)
    246.   If MainKeyHandle Then
    247.     rtn = RegOpenKeyEx(MainKeyHandle, SubKey, 0, KEY_WRITE, hKey)
    248.     If rtn = ERROR_SUCCESS Then
    249.       lDataSize = Len(Value)
    250.       ReDim ByteArray(lDataSize)
    251.       For i = 1 To lDataSize
    252.         ByteArray(i) = Asc(Mid$(Value, i, 1))
    253.       Next
    254.       rtn = RegSetValueExB(hKey, Entry, 0, REG_BINARY, ByteArray(1), lDataSize)
    255.       If Not rtn = ERROR_SUCCESS Then
    256.         If DisplayErrorMsg = True Then
    257.           MsgBox ErrorMsg(rtn)
    258.         End If
    259.       End If
    260.       rtn = RegCloseKey(hKey)
    261.     Else
    262.       If DisplayErrorMsg = True Then
    263.         MsgBox ErrorMsg(rtn)
    264.       End If
    265.     End If
    266.   End If
    267. End Function
    268.  
    269. Public Function CreateEntryToSystemPanel(GUID As String, _
    270.   Titel As String, _
    271.   ToolTipText As String, _
    272.   IconDatei As String, _
    273.   FileToOpen As String)
    274.  
    275.  
    276.   UpdateKey HKEY_CLASSES_ROOT, "CLSID\" & GUID, "", Titel
    277.   UpdateKey HKEY_CLASSES_ROOT, "CLSID\" & GUID, "InfoTip", ToolTipText
    278.   UpdateKey HKEY_CLASSES_ROOT, "CLSID\" & GUID & "\DefaultIcon", "", IconDatei
    279.   UpdateKey HKEY_CLASSES_ROOT, "CLSID\" & GUID & "\InProcServer32", "", "shell32.dll"
    280.   UpdateKey HKEY_CLASSES_ROOT, "CLSID\" & GUID & "\InProcServer32", "ThreadingModel", "Apartment"
    281.   UpdateKey HKEY_CLASSES_ROOT, "CLSID\" & GUID & "\Shell\Open\Command", "", FileToOpen
    282.  
    283.   Dim sKey As String
    284.   sKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\"
    285.  
    286.   UpdateKey HKEY_LOCAL_MACHINE, sKey & "Desktop\NameSpace\" & GUID, "", ""
    287.   UpdateKey HKEY_LOCAL_MACHINE, sKey & "ControlPanel\NameSpace\" & GUID, "", ""
    288.   CreateKey "HKEY_CLASSES_ROOT\CLSID\" & GUID & "\ShellFolder"
    289.   SetBinaryValue "HKEY_CLASSES_ROOT\CLSID\" & GUID & "\ShellFolder", _
    290.     "Attributes", Chr$(&H0) + Chr$(&H0) + Chr$(&H0) + Chr$(&H0)
    291. End Function
    292.  
    293. Public Function DeleteEntryFromSystemPanel(GUID As String)
    294.   Dim sKey As String
    295.   sKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\"
    296.  
    297.   DeleteKey "HKEY_CLASSES_ROOT\CLSID\" & GUID
    298.   DeleteKey "HKEY_CLASSES_ROOT\CLSID\" & GUID & "\DefaultIcon"
    299.   DeleteKey "HKEY_CLASSES_ROOT\CLSID\" & GUID & "\InProcServer32"
    300.   DeleteKey "HKEY_CLASSES_ROOT\CLSID\" & GUID & "\Shell\Open\Command"
    301.   DeleteKey "HKEY_CLASSES_ROOT\CLSID\" & GUID & "\ShellEx\PropertySheetHandlers\" & GUID & ""
    302.   DeleteKey "HKEY_CLASSES_ROOT\CLSID\" & GUID & "\ShellFolder"
    303.   DeleteKey "HKEY_LOCAL_MACHINE\" & sKey & "\Desktop\NameSpace\" & GUID
    304.   DeleteKey "HKEY_LOCAL_MACHINE\" & sKey & "\ControlPanel\NameSpace\" & GUID
    305. End Function

    Source:
    http://www.vbcode.com/Asp/showsn.asp?theID=11046

    I hope that helps, you are correct when you say that it rights it to the registery.

    Thanks!

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