Home arrow Visual Basic arrow Page 3 - Finding Default App Icons With Visual Basic
VISUAL BASIC

Finding Default App Icons With Visual Basic


Using the Windows API it's easy to find the default icon for any file extension. Phil shows us exactly how it's done in this comprehensive article.

Author Info:
By: Phil Couling
Rating: 5 stars5 stars5 stars5 stars5 stars / 42
December 27, 2002
TABLE OF CONTENTS:
  1. · Finding Default App Icons With Visual Basic
  2. · How It's All Done
  3. · The Registry
  4. · The Complete Code
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Finding Default App Icons With Visual Basic - The Registry
(Page 3 of 5 )

Firstly, we need to convert the filename into just a file extension. That is, remove everything before the ".":

TempFileName = Right(FileName, Len(FileName) - InStrRev(FileName, ".") + 1)

Now we take a look at the registry and see if it has any record of this file type (it is, of course, possible that it doesn't):

lngError = RegOpenKey(HKEY_CLASSES_ROOT, TempFileName, lngRegKeyHandle)
If lngError Then GoTo No_Icon 'we do not even have a valid extension so lets NOT try to find an icon!
lngStringLength = 260
'Make space for the incoming string
strProgramName = Space$(260)
'Get the key value:
lngError = RegQueryValueEx(lngRegKeyHandle, vbNullString, 0, 0, strProgramName, lngStringLength)
If lngError Then
'if there's an error then BIG TROUBLE so lets use the normal "windows" icon
'the world may be about to end (or its just an error) but we'll clean up anyway
lngError = RegCloseKey(lngRegKeyHandle)
GoTo No_Icon
End If
'if this generates an error then we can't do anthing about it anyway
lngError = RegCloseKey(lngRegKeyHandle)
'Cut the name down to size
strProgramName = Left(strProgramName, lngStringLength - 1)


You will notice that even when I am not going to use it I still make "lngError" receive any errors. I have left this in because it's useful for debugging.

You will notice that if any errors occur in this code I use the line "GoTo No_Icon". This is because even if we can't get a default icon for this file type, we will still want to display something, i.e. the default windows icon:



Ok, now we have the key name of our second registry key (stored in the strProgramName variable). What we need to do now is get the location for the default icon. The value that we want is held inside the sub-key "DefaultIcon". So very similarly to the last piece of code, we need to get this value:

'Use the value of the last key in the name of the next one (strProgramName )
lngError = RegOpenKey(HKEY_CLASSES_ROOT, strProgramName & "\DefaultIcon", lngRegKeyHandle)
'there is no icon for this extension so lets NOT try to load what doesn't exist!
If lngError Then GoTo No_Icon
'The rest is just the same as before
lngStringLength = 260
strDefaultIcon = Space$(260)
lngError = RegQueryValueEx(lngRegKeyHandle, vbNullString, 0, 0, strDefaultIcon, lngStringLength)
If lngError Then
lngError = RegCloseKey(lngRegKeyHandle)
GoTo No_Icon
End If
lngError = RegCloseKey(lngRegKeyHandle)
strDefaultIcon = Trim$(Left(strDefaultIcon, lngStringLength - 1))


Drawing the Icon
So we now have the location of the default icon. There is just one problem with this: the icon may be stored in a .exe, .dll or .ico file. This is not as much of a problem as it may sound. But we just need to do one thing first. The default icon it stored in the form of: "C:\BlahBlah\Blah.ico, 42".

We need to split the number up from the location:

'Find the comma
intN = InStrRev(strDefaultIcon, ",")
'We MUST have an icon number and it will be after the ",": NO COMMA NO DEFAULT ICON
If intN < 1 Then GoTo No_Icon
'What number is after the comma
lngIconNumber = Trim$(Right(strDefaultIcon, Len(strDefaultIcon) - intN))
'We only want whatever is before the comma in the file name
strDefaultIcon = Trim$(Left(strDefaultIcon, intN - 1))


So now we just need to draw the icon. This is where we need the "Device Context" (hDC) of our picture box:

Draw_Icon:
'Extract the Icon
lngIcon = ExtractIcon(App.hInstance, strDefaultIcon, lngIconNumber)
'if 1 or 0 then after all that the Icon could not be retrieved
If lngIcon = 1 Or lngIcon = 0 Then GoTo No_Icon
lngError = DrawIcon(Picture_hDC, 0, 0, lngIcon) 'Draw the icon in the box
'If that was unsucessful then we can't do anything about it now!
lngError = DestroyIcon(lngIcon)
'Again we can't correct any errors now
Exit Sub


The Contingency
In case we can't find the default icon, we need to display the Windows icon. Here's how:

No_Icon:
'No icon could be found so we use the normal windows icon
'This icon is held in shell32.dll in the system directory, Icon 0
strDefaultIcon = Space(260)
lngStringLength = GetSystemDirectory(strDefaultIcon, 260)
strDefaultIcon = Left(strDefaultIcon, lngStringLength) & "SHELL32.DLL"
lngIconNumber = 0
GoTo Draw_Icon
End Sub


The Windows icon will be held in the file "SHELL32.DLL", which is in the Windows system folder. So, we get the system directory and use that (along with "SHELL32.DLL") for the filename and then jump back to the "Draw_Icon" portion of code.

At this point I am just going to add an extra piece of code that will handle EXE's. This is because EXE's have no default icon but it's probably not best to just show the windows icon. So let's use the icon that Windows normally uses for EXE's:



Add this near the begging of your code, under "TempFileName = Right..." and above "lngError = RegOpenKey..."

If LCase(TempFileName) = ".exe" Then
strDefaultIcon = Space(260)
lngStringLength = GetSystemDirectory(strDefaultIcon, 260)
strDefaultIcon = Left(strDefaultIcon, lngStringLength) & "SHELL32.DLL"
lngIconNumber = 2
GoTo Draw_Icon
End If


This code is the same code used for getting the Windows icon except that we now want icon 2 instead of 0.
blog comments powered by Disqus
VISUAL BASIC ARTICLES

- Developing an XML Web Service Using Visual S...
- Creating an HTML File List with VB
- Fun with Email: VB6, CDO, MAPI, and a Remote...
- Extranet/Intranet Dictionary Cracker in VB
- Finding Default App Icons With Visual Basic
- Registry Fever With Visual Basic
- Implementing An ADO Data Control With VB6
- Printing With Visual Basic
- MSMQ Part 1/2: Architecture and Simple Imple...
- Magnifying The Desktop With Visual Basic
- Sending Email With MAPI Components in Visual...
- Two Person Chat With The Winsock Control And...
- A Real-Time ActiveX News Control
- Accessing the Windows API in Visual Basic

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials