|
Creating Desktop Shortcuts Using Visual Basicby Ron Petrusha, coauthor of VBScript in a Nutshell, 2nd Edition04/13/2004 |
Once the Visual Basic 6.0 developer leaves the familiar array of features and services offered by the Visual Basic environment and by the more commonly used object models (such as DAO, ADO, or the more commonly used Microsoft Office object models), learning how to perform a particular task and selecting the appropriate technology to do it often becomes confusing. This is particularly true of creating desktop shortcuts. In this article, we'll examine how you can use Visual Basic to create desktop shortcuts programmatically (when your application or component is running) and as part of an installation program (using the Package and Deployment Wizard included with VB6).
Microsoft offers two technologies that the Visual Basic developer can use to create shortcuts. The first is the Shell Library, a collection of COM automation objects and standard C/C++ functions found in Shell32.dll. The library, however, is really geared toward the C/C++ developer. Much more accessible and straightforward is the Windows Scripting Host Object Library (wshom.dll), which in this case, merely wraps the IShellLink interface of Shell32.dll. Although Windows Script Host (WSH) is most commonly called by scripted languages, such as VBScript or JScript, most of its object model is also accessible from Visual Basic. WSH has the advantage of being a very "flat" object model; you don't have to navigate through an extensive hierarchy of objects to either instantiate or retrieve the object in which you're interested.
WSH offers two different objects that can be used in creating desktop shortcuts. The first is the WshShortcut object, which represents a shortcut to a file system object on the local computer or network. The second is the WshURLShortcut object, which represents a shortcut to an Internet resource. Either of these two objects is returned by the CreateShortcut method of the WSH Shell object. CreateShortcut requires a single argument, PathLink, the path and filename of the link file to be created or retrieved. So, the general code needed to instantiate a shortcut object is:
Dim sShortcutPath As String
Dim oShortcut As Object ' Use late binding to accommodate both
' WshShortcut and WshURLShortcut objects
Dim oShell As New WshShell
sShortcutPath = < path and filename of link file >
Set oSh = oScr.CreateShortcut(fn)
Note that this code uses late binding (oShortcut is declared to be of type Object, Visual Basic's generic object type, rather than the more specific WshShortcut or WshURLShortcut), since we don't know beforehand whether we're going to create a shortcut or an Internet shortcut. If you know in advance what type of shortcut your code will handle, you can, of course, use early binding. Alternatively, you can simply define your object to be of type WshShortcut and link it either to a local file system resource or to an Internet resource, since the target of a WshShortcut object can be defined either by a local filesystem path or by an Internet URL.
|
Related Reading VBScript in a Nutshell |
Whether CreateShortcut returns a WshShortcut object or a WshURLShortcut object depends on the file extension supplied with the PathLink argument. If the file extension is .lnk, CreateShortcut returns a WshShortcut object; if .url, it returns a WshURLShortcut object. Any other file extension generates an automation error. (You can, incidentally, extract the file extension easily by using the GetExtensionName method of the FileSystemObject object, found in the Microsoft Scripting Runtime Library, scrrun.dll. So, you could determine what type of link you're working with by using code such as the following:
Dim sShortcutPath As String, sExtension As String
Dim fs As New FileSystemObject
Dim oShell As New WshShell
sShortcutPath = InputBox("Enter path and filename of link file: ")
If sShortcutPath <> "" Then
sExtension = fs.GetExtensionName(sShortcutPath)
Select Case sExtension
Case "lnk"
Dim oShortcut As WshShortcut
Set oShortcut = oShell.CreateShortcut(sShortcutPath)
Case "url"
Dim oURLShortcut As WshURLShortcut
Set oURLShortcut = oShell.CreateShortcut(sShortcutPath)
Case Else
' user input an invalid path or filename; display an error and
' exit
Exit Sub
End Select
End If
If the argument supplied to the CreateShortcut object is the path and name of an existing file, then the property values of the WshShortcut or WshURLShortcut object are updated from the existing file. Otherwise, a new file is created. The WshShortcut object has the members shown in Table 1.
Table 1. Members of the WshShortcut Object
| Member Name | Data Type | Description |
|---|---|---|
Arguments Property |
String |
Any command-line arguments that are to be passed to the executable (defined by the |
Description Property |
String |
A text description of the file. It is displayed in the Comments textbox of the link file's Properties dialog. |
FullName Property |
String |
Read-only. The path and filename of the link file. The property value is set by the PathLink parameter of the WshShell object's CreateShortcut method. |
Hotkey Property |
String |
A key combination that will automatically invoke the link. It typically consists of a special key (Alt, Ctrl, Shift), a plus sign, and a regular key or function key (F1, F2, etc.). For instance, the following code defines the Alt-F11 control key sequence as the hotkey for a link file: oShortcut.Hotkey = "Alt+F11"
The |
IconLocation Property |
String |
The path and filename of an icon resource in an executable or dynamic link library, along with its zero=based index in the .exe or .dll. For instance, the following code assigns the third icon in Shell32.dll to the link file:oShortcut.IconLocation = "C:\Windows\System32\Shell32.dll,2
Note that the value of the |
Load Method |
None | A hidden, private method. During instantiation, Load is called automatically by the |
RelativePath Property |
String |
Write-only. Defines a target path and filename that are relative to the shortcut's path. This allows the target to be found if both shortcut and target are moved to new locations with the same relative relationship. |
Save Method |
None | Saves the current values of the link file. Its syntax is: |
TargetPath Property |
String |
The target of the link. That is, the path and name of the executable, file, or folder to which the shortcut refers. For desktop shortcuts, this is typically the application launched when the user clicks on the shortcut. |
WindowStyle Property |
Long |
An integer that defines the target's window style. This can also be a selected member of the |
It's important to note that, once you modify an existing shortcut's property values or assign property values to a new shortcut, the new shortcut property values are not actually written to the shortcut file until you call the Save method.
Pages: 1, 2 |



