Consultancy and Programming
Advice, tips, techniques and downloads for Visual Foxpro developers.
This article applies to Visual Foxpro 6.0 and earlier. If you are using a later version, please refer to the note at the end of the article.
If you have ever used Visual FoxPro's Setup Wizard, you will know that this handy tool will cheerfully install a shortcut to your application in the user's Start menu. What the wizard can't do is install a shortcut on the user's desktop. Nor does VFP itself provide any means of creating desktop shortcuts.
Fortunately, there are other ways of achieving that goal. In this article, we will show you two of them. The first is a 'manual' method which you can use in conjuntion with the Setup Wizard. It is easy to implement, provided you have certain information about the user's system. The second technique requires some programming, but is much more generic than the first.
For this first method, you will need to know (i) the path to the directory on the user's system which will hold your application's executable file, and (ii) the path to the user's Desktop directory. This latter directory is usually a sub-directory of the Windows directory. For a typical Windows 9x system with Windows installed on the C drive, the Desktop directory will be c:\windows\desktop, but you will need to check this in each case.
Once you have the required information, follow these steps:
COPY "C:\MyApp\MyApp.LNK" "C:\Windows\Desktop"
If all goes well, the user will see the shortcut on the desktop as soon as they have run your Setup program. The shortcut will include the application's icon - the one which you specified in the Project Info dialogue - or the default fox icon if you have not specified an application icon. (For more information about specifying application icons, see the Foxstuff article, Do your application icons always show up?).
If you prefer a more generic approach - one that does not rely on knowing particular directory names on the target computer - this second technique will be a better choice. It uses the Windows Scripting Host (WSH) to create the shortcut and requires you to write a few lines of Visual FoxPro code.
The drawback of this approach is that it will only work if the WSH is already installed on the user's computer. That won't be a problem for users of Windows 98, Me, NT or 2000, as these systems all have WSH built in. But Windows 95 users will only have it if they have installed Internet Explorer 3.0 or later.
To access the WSH from VFP, you first instantiate it as an object, for example like so:
oWsh = CREATEOBJECT("wscript.shell")
In this example, oWsh is a variable which will hold an object reference to the WSH.
Th WSH object has a method, called SpecialFolders(), which can be used to determine the name and path of any of the special system folders in Windows. These include Desktop, StartMenu, Favorites, My Documents and Fonts. So to get a path to the Desktop folder, you do this:
cDeskPath = oWsh.SpecialFolders("desktop")
The WSH object also has a CreateShortcut() method, which does just what its name suggests. This takes as a parameter the name and path of the shortcut which you want to create. The following code will create a shortcut called MyApp.LNK in the Desktop directory:
oShort = ; oWsh.CreateShortcut(cDeskpath+"\MyApp.LNK")
After this code is executed, oShort will contain an object reference to the shortcut.
The shortcut object has a TargetPath property which contains the qualified filename of the file to which the shortcut points. The following line of code will make the shortcut point to MyApp.EXE in the c:\MyApp directory:
oShort.TargetPath = "c:\MyApp\MyApp.EXE"
The object also has Description, Hotkey, IconLocation and WorkingDirectory properties which can be set in the same way.
The final step is to save the shortcut, which you do like so:
After the above code has been executed, the shortcut should appear on the desktop - again using the icon specified in the Project Info dialogue. Using the same general technique, you can create shortcuts in any of the other special folders, or indeed anywhere in the user's file system. You can write the code in your main VFP application, or in a separate utility which can be executed as the post-setup executable in the Setup Wizard.
Starting with VFP 7.0, the Setup Wizard has been replaced by a limited version of the InstallShield setup program. This program fully supports the installation of desktop shortcuts. Simply click on Shortcuts/Folders in Step 3 (Configuring the Target System). You will see a property sheet in which you can enter the shortcut's description, target path, icon file, working directory and similar details.
Mike Lewis Consultants Ltd. April 2001. Revised August 2001 and November 2001.
Thanks to Michael D. Smith of Doncaster Office Services for corrections to the original version of this article.)
FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).
The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.
© Copyright Mike Lewis Consultants Ltd.