File Extensions: Finding the default Icon

Declares

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 make this in a module and refer back to the subroutine 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 generate an event)

So lets 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 accept that what it is doing 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 (Windows APIs) available here.

Now we need to declare the function and all its private/temporary 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

You might also like...

Comments

About the author

Phil Couling United Kingdom

Software developer, working in Derby, England. Currently I'm mostly developing in C++ with a combination of Borland, Visual Studio and MinGW.

Much of my work at the moment is with Databa...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Walking on water and developing software from a specification are easy if both are frozen.” - Edward V Berard