Visual Basic Win32 Shell Routines
SHGetSpecialFolderLocation: Create an IE Shortcut (.url)
Posted:   Friday July 25, 1997
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch

Here's a quick (not to mention dirty) method of adding an Internet shortcut to the user's IE Favourites folder or desktop.

The shortcuts for Microsoft's Internet Explorer use a simple ini-style file containing a variety of information, but basically consists of a fixed header, and the path to the desired URL. Shortcuts for IE4/5/6 can contain more information such as the icon to use and the date added, but they follow same basic design and extraneous information is ignored by earlier IE versions.  Therefore, using the technique below and specifying the target 'special folder location', you can create your own IE-compatible shortcut on any user's system.

 BAS Module Code

 Form Code
To the form, add the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const ERROR_SUCCESS As Long = 0
Private Const CSIDL_FAVORITES As Long = &H6

Private Declare Function SHGetPathFromIDList Lib "shell32" _
    Alias "SHGetPathFromIDListA" _
   (ByVal pidl As Long, _
    ByVal pszPath As String) As Long

Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
   (ByVal hwndOwner As Long, _
    ByVal nFolder As Long, _
    pidl As Long) As Long
Private Declare Sub CoTaskMemFree Lib "ole32" _
   (ByVal pv As Long) 

Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
End Sub

Private Function GetSpecialPath(CSIDL As Long) As String

   Dim path As String
   Dim pidl As Long
  'get the pidl to the the specified folder item     
   If SHGetSpecialFolderLocation(Me.hwnd, CSIDL, pidl)= NOERROR Then
      path = Space$(512)
      Call SHGetPathFromIDList(ByVal pidl, ByVal path)  
      GetSpecialPath = Left$(path, InStr(path, Chr$(0)) - 1)
      Call CoTaskMemFree(pidl)
      Exit Function

    End If

   GetSpecialPath = ""

End Function

Private Sub Command1_Click()

   Dim CSIDLpath As String
   Dim nameofLink As String
   Dim ff As Integer
  'get the user's 'special' folder location for Favourites,
  'and append a trailing slash   
   CSIDLpath = QualifyPath(GetSpecialPath(CSIDL_FAVORITES)) 
  'set the filename of the new link
   nameofLink = "VBnet Home Page.url"
  'despite it being an ini-style file, we can use simple
  'VB file open and print statements to create the shortcut   
   ff = FreeFile
  'open the file for writing   
   Open CSIDLpath & nameofLink For Output As #ff
      Print #ff, "[Default]"
      Print #ff, "BASEURL="
      Print #ff, "[InternetShortcut]"
      Print #ff, "URL="

      Print #ff, "IconFile="
      Print #ff, "IconIndex=1"    
   Close #ff

End Sub

Private Function QualifyPath(sPath As String) As String
  If Len(sPath) > 0 Then
      If Right$(sPath, 1) <> "\" Then
         QualifyPath = sPath & "\"
         QualifyPath = sPath
      End If
      QualifyPath = ""
   End If
End Function
By passing CSIDL_DESKTOPDIRECTORY in the GetSpecialPath() call, the path to the user's system desktop folder is returned (i.e. c:\windows\desktop), resulting in the shortcut being created on the desktop. Obviously this is for IE owners only; you will want to check for the default browser, or ask if Internet Explorer is installed. Netscape uses a different file method to store its bookmarks.

This method of installing a shortcut guarantees that on an NT system, or Win95/Win98 system with policies in force, that the shortcut is applied to the current user's desktop and Favourites folders.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter