Creating Desktop Shortcuts Using Visual Basic
Pages: 1, 2
The WshURLShortcut object is similar to the WshShortcut object, except that it has only two properties: the FullName property, which defines the path and name of the Internet shortcut; and the TargetPath directory, which defines the resource to which the Internet shortcut points. The WshURLShortcut object also supports the Load method (which, as in the case of the WshShortcut method, is private and cannot be called from Visual Basic code) and the Save method.
Shortcut Viewer, a downloadable Visual Basic application that allows shortcuts to be created and edited, illustrates the use of the WshShortcut and WshURLShortcut objects. Although we won't present the code here (you can download the file and examine it for yourself), several comments about the code are in order. By default, the utility's File New and File Open dialogs create and retrieve shortcut files found on the user's desktop. Although there are several ways to retrieve this location, the application uses the SpecialFolders property of the WshShell object, as follows:
strDesktop = oShell.SpecialFolders("Desktop")
Along with the folder representing the user's desktop items, Windows has a shared folder containing desktop items for all users of a system. Its path can be retrieved as follows:
strDesktop = oShell.SpecialFolders("AllUsersDesktop")
There are also a number of other locations where shortcuts are commonly stored, all of which can be retrieved using the SpecialFolders property. These include the following:
- The Favorites folder, which is indicated by the string "Favorites".
- The user's Start menu , which is indicated by the string "StartMenu".
- The Start menu of all users, which is indicated by the string "AllUsersStartMenu".
- The users Startup folder (so that the shortcut's target is launched whenever the user logs in to the system), which is indicated by the string "Startup".
- The Startup folder for all users (so that the shortcut's target is launched whenever any user logs in to the system), which is indicated by the string "AllUsersStartup".
- The most recently used (MRU) documents list, which appears when the user selects the Documents item on the Start menu. It is indicated by the string "Recent".
Although the WshShortcut object allows you to select the shortcut file's icon from an executable or DLL containing icon resources, the WshURLShortcut object does not. (Interestingly, though, the Properties dialog for an Internet Shortcut does allows you to change the icon.) Instead, the location of a default icon for the shortcut is retrieved from the system registry. This location is determined by the default value stored to the HKEY_CLASSES_ROOT\InternetShortcut\DefaultIcon key.
Finally, since the CreateShortcut method generates an error if we attempt to create or retrieve a shortcut with an extension other than .lnk or .url, it is important that we verify the file extension prior to calling the method. We can do this easily using the GetExtensionName method of the FileSystemObject object, which is part of the Microsoft Scripting Runtime Library.
Although it's useful to create shortcuts programmatically from a standard Visual Basic application, it is most common to create desktop shortcuts programmatically from an installation program. By default, the Package and Deployment Wizard bundled with Visual Basic does not allow you to create desktop shortcuts. However, you can easily add this capability by customizing the Setup1.vbp project used by the wizard. Typically, the wizard's source files are found in C:\Program Files\Microsoft Visual Studio\VB98\Wizards\PDWizard\Setup1, while its executables are found in C:\Program Files\Microsoft Visual Studio\VB98\Wizards\PDWizard\Setup1. Be sure to make a backup copy of the project's files before beginning modification.
Interestingly, the Package and Deployment Wizard features a dynamic link library, vb6stkit.dll, that includes fCreateShellLink, a function capable of creating shortcuts. It is intended primarily to add items to program groups or to the Start menu, but it is poorly documented and does not appear to work reliably when creating desktop shortcuts. Rather than relying on it, we'll make use of Windows Script Host and the Scripting Runtime Library. Hence, you should add references to these two libraries to the Setup1 project.
The Package and Deployment Wizard invokes the code responsible for creating a desktop shortcut when the user clicks the Continue button of a form named frmGroup. We should be able to confine our modifications to it.
If the installation program you're creating is going to ask the user whether it should create a desktop shortcut, you'll have to modify the installation interface. In the case of a desktop shortcut, the installation program would need to know whether to create a shortcut only on the user's desktop, or on all users of a system. So, to make this change to the interface, begin by lengthening the form so that it is long enough to accommodate a frame containing two option buttons underneath the lstGroups list box. Move the two command buttons so that they are near the bottom of the form. Next, add the frame and place the two option buttons inside it. Set the following properties:
| Control | Property | Value |
|---|---|---|
| Frame | Name | fraDesktop |
| Caption | Create Desktop Shortcut | |
| First Option Button | Name | optCurrent |
| Caption | for Current User | |
| Second Option Button | Name | optAll |
| Caption | for All Users |
The completed form as it appears in Design Mode should look something like the one shown in Figure 1.

Figure 1. Here's how the completed form will appear in Design Mode.
Our code modifications can be confined to the following portion of the cmdContinue_Click event procedure, which is executed after items are successfully added to a program group:
Else
'
' The group got created ok, so unload Choose Program Group dialog
' and continue on with setup.
'
Unload Me
End If
The following is the complete code block after modifications, with new lines appearing in bold:
Else
' Check whether to add desktop shortcut
If Me.optCurrent.Value Or Me.optAll.Value Then
Dim sDesktop As String, sTarget As String, sAppName As String
Dim oShell As New WshShell
Dim oShortcut As WshShortcut
Dim oFS As New FileSystemObject
' Get name of target
sTarget = gstrDestDir & gstrAppExe
' Get Desktop location
If Me.optCurrent.Value Then
sDesktop = oShell.SpecialFolders("Desktop")
Else
sDesktop = oShell.SpecialFolders("AllUsersDesktop")
End If
' Get root application name
sAppName = oFS.GetBaseName(sTarget)
' Form shortcut filename
sDesktop = sDesktop & "\" & sAppName & ".lnk"
' Create shortcut
Set oShortcut = oShell.CreateShortcut(sDesktop)
oShortcut.TargetPath = sTarget
oShortcut.Save
End If
'
' The group got created ok, so unload Choose Program Group dialog
' and continue on with setup.
'
Unload Me
End If
Except for the use of some of the global variables defined by the Package and Deployment Wizard, this code is fairly straightforward. We use the SpecialFolders property of the Shell object to get the location of the user's desktop or of the items common to all users' desktops. We also extract the root filename of the target application by calling the FileSystemObject object's GetBaseName method. Then it's simply a matter of performing some string concatentation, and creating and saving our shortcut object.
Ron Petrusha is the author and coauthor of many books, including "VBScript in a Nutshell."
Return to WindowsDevCenter.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 8 of 8.
-
How to uninstall the shortcut you create
2007-12-20 17:53:44 mARSh [Reply | View]
Hi
Very excellent Tip.
But what is the best way remove the this desktop shortcut once software is uninstall?
-
Shortcuts without WindowsScripting Object?
2006-02-24 07:23:59 JRCSystems [Reply | View]
Here's a question:
Without using vb5 or 6 stkit.dll, and without
using WindowsScriptingObject, how do you create a desktop shortcut/shell .lnk file ?????
Why without any of the above?
a) I doubt a C programmer references a vb5stkit
which leads me to believe there must an entry-
point into a REAL system DLL that does that.
b) Depending on what software is installed on
the user computer, accessing the Windows-
Scripting object, displays nasty messages:
Like one from Norton saying your program is a
malicious script and defaults the operation
to "Do Not Allow".
So, how does a cleanly written, professional program create a desktop shortcut?
Thanks.
john@jrcsystems.com
-
Quick and Easy Solution
2005-10-08 06:27:13 cpgguru [Reply | View]
'*****************************************************
'* Create Shortcut to URL Link on AllUsersDesktop '*
'* scripted by: cpgguru '*
'*****************************************************
URL = "URL=http://www.oreillynet.com"
DIM fso, File
Dim WSHShell, DesktopPath
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
'Read desktop path using WshSpecialFolders object - "Desktop","AllUsersDesktop","Favorites","StartMenu","AllUsersStartMenu"
'"Startup","AllUsersStartup"
DesktopPath = WSHShell.SpecialFolders("AllUsersDesktop")
Set File = fso.CreateTextFile(DesktopPath & "\oreillynet.url", True)
File.WriteLine("[InternetShortcut]")
File.Write (URL)
'File.Write ("same line write")
File.Close
-
Creating Desktop shortcuts
2005-06-02 03:15:27 Jooey [Reply | View]
I'd like to create a url type shortcut to put on my website that when saved will have it's own specific image. Is there an easy way of packaging the image and shortcut ? any help is much appreciated..
-
no
2004-04-15 04:36:42 bry [Reply | View]
I've noticed problems (like crashes etc.) in programs that are relying on different versions of wscript. And wscript is not the kind of thing that one installs or updates on a user's machine.
So I definitely would not want to
rely on WScript for a professional program, which is the kind of program I can see having a need to write a desktop shortcut.
anyway creating url type shortcuts can be done using a text file, don't believe me open notepad and write the following:
[InternetShortcut]
URL=http://www.oreillynet.com/
save it as oreilly.url and you will have a shortcut.






somwwhat i achieved it (using iwshruntime.dll),
but running in windows 2000 it gives an error,
com object not suppoting,is there any otherway to create shortcut in 2000