Home arrow Visual Basic arrow Page 2 - 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 - How It's All Done
(Page 2 of 5 )

All the registry keys that we are going to use are stored under HKEY_CLASSES_ROOT. The registry stores them in this form:
  • Every file extension is given a key. This key will be exactly the same as the file extension including the dot (eg: HKEY_CLASSES_ROOT\.bmp).
  • This keys "default" value refers to an application's registry key.
  • Every application is given a key (or a group of keys if the program is associated with multiple file types). This is not necessarily the same as its "EXE name", but in this key, among other things, there is a value that contains the location of the default file icon for this file type. For example: a ".frm" file. You should recognize this as a VB form.
This is what my registry has to say about a ".frm":



Your registry may differ, but the format should still be the same. The important thing to notice is how we got from ".frm" to "C:\program files\Microsoft Visual Studio\VB98\vb6.exe,1"

Finding the Default Icon
We're going to make a function which, when given a file name, will find the default icon for it's type (by its extension) and then draw it onto an object. It needs two inputs: the file name and the "hDC" of the object that it is going to draw onto.

My advice to you would be to add this code into a module and refer back to the subroutine that we are about to write (GetDefaultIcon). You will also need (for testing purposes at least) a form containing:
  • A picture box (With AutoRedraw = true). You could draw onto most things but they must have a device context (hDC value at runtime).
  • A text box (or some way of entering a file name)
  • A command button (or something to raise an event)
So let's start by declaring every thing:

'For looking at registry keys
'To: Open key ready to look at
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
'To: Look at key
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Any, lpcbData As Long) As Long
'To: Close the key when it's finished with
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const KEY_READ = &H20019 'To allow us to READ the registery keys

'For Drawing the icon
'To: Retrieve the icon from the .EXE, .DLL or .ICO
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
'To: Draw the icon into our picture box
Private Declare Function DrawIcon Lib "user32.dll" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal hIcon As Long) As Long
'To: Clean up after our selves (destroy the icon that "ExtractIcon" created)
Private Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Long) As Long

'For Finding the System folder
Private Declare Function GetSystemDirectory Lib "kernel32.dll" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long


If none of this code means anything to you then don't worry too much. Just know that it is adding seven extra functions to this program that aren't normally in VB. If you are interested then there is an article written by James Crowley on the subject of Windows API's available here.

Next, we need to declare the function and all of its private variables:

Private Sub GetDefaultIcon(FileName As String, Picture_hDC As Long )
Dim TempFileName As String 'Never manipulate an input unless it doubles as an output
Dim lngError As Long 'For receiving error numbers
Dim lngRegKeyHandle As Long 'Stores the "handle" of the registry key that is currently open
Dim strProgramName As String 'Stores the contents of the first registry key
Dim strDefaultIcon As String 'Stores the contents of the second registry key
Dim lngStringLength As Long 'Sets / Returns the length of the output string
Dim lngIconNumber As Long 'Stores the icon number within a file
Dim lngIcon As Long 'Stores the "Icon Handle" for the default icon
Dim intN As Integer 'For any temporary numbers

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