1. Technology

Create an Internet Shortcut (.URL) file using Delphi

By

Unlike regular .LNK shortcuts (that point to a document or an application), Internet Shortcuts point to an URL (web document). Here's how to create an .URL file, Internet Shortcut, using Delphi.

The Internet Shortcut object is used to create shortcuts to Internet sites or web documents. Internet shortcuts are diverse from regular shortcuts (which contain data in a binary file) that point to a document or an application. Such text files with a .URL extension have their content in INI file format.

The easiest way to look inside an .URL file, is to open it inside Notepad. The content (in its simplest form) of an Internet Shortcut could look like this:

 [InternetShortcut]
 URL=http://delphi.about.com
 
As you can see, .URL files have an INI file format. The URL represents the address location of the page to load. It must specify a fully qualifying URL with the format protocol://server/page.
For other possible fields, I suggest you to take a look at "An Unofficial Guide to the URL File Format".

New ... Internet Shortcut

You can easily programmatically create an Internet shortcut if you have the URL of the page to which you want to link. When double-clicked, the default browser is launched and displays the site (or a web document) associated with the shortcut.

Here's a simple Delphi function to create an .URL file. The CreateInterentShortcut procedure creates a URL shortcut file with the provided file name (FileName parameter) for the given URL (LocationURL), overwriting any existing Internet Shortcut with the same name.

 uses IniFiles;
 ...
 procedure CreateInternetShortcut(const FileName, LocationURL : string) ;
 begin
   with TIniFile.Create(FileName) do
   try
     WriteString(
        'InternetShortcut',
        'URL',
        LocationURL) ;
   finally
     Free;
   end;
 end; (*CreateInterentShortcut*)
 
Here's a sample usage:
 //create an .URL file named "About Delphi Programming"
 //in the root folder of the C drive
 //let it point to http://delphi.about.com
 CreateInterentShortcut('c:\About Delphi Programming.URL ', 'http://delphi.about.com ') ;
 
A few notes

Specifying the .URL Icon

One of the neater features of the .URL file format is that you can change the shortcut's associated icon. By default the .URL will carry the icon of the default browser. If you want to change the icon, you only have to add two additional fields to the .URL file, as in:
 [InternetShortcut]
 URL=http://delphi.about.com
 IconIndex=0
 IconFile=C:\MyFolder\MyDelphiProgram.exe
 
The IconIndex and IconFile fields let you specify the icon for the .URL shortcut. The IconFile could point to your application's exe file (IconIndex is the index of the icon as a resource inside the exe).

Internet Shortcut ... to open a regular document or an application

Being called an Internet Shortcut, an .URL file format does not permit you to use it for something else - such as a standard application shortcut.

Note that the URL field must be specified in the protocol://server/page format. For example, you could create an Internet Shortcut icon on the Desktop, that points to your program's exe file. You only need to specify the "file:///" for the protocol. When you double click on such an .URL file, your application will be executed. Here's an example of such an "Internet Shortcut":

 [InternetShortcut]
 URL = file:///c:\MyApps\MySuperDelphiProgram.exe
 IconIndex = 0
 IconFile = C:\MyFolder\MyDelphiProgram.exe
 
Here's a procedure that places an Internet Shortcut on the Desktop, the shortcut points to the *current* application. You can use this code to create a shortcut to your program:
 uses IniFiles, ShlObj;
 ...
 function GetDesktopPath: string;
 //get the location of the Desktop folder
 var
   DesktopPidl: PItemIDList;
   DesktopPath: array [0..MAX_PATH] of Char;
 begin
   SHGetSpecialFolderLocation(0, CSIDL_DESKTOP, DesktopPidl) ;
   SHGetPathFromIDList(DesktopPidl, DesktopPath) ;
   Result := IncludeTrailingPathDelimiter(DesktopPath) ;
 end; (*GetDesktopPath*)
 
 procedure CreateSelfShortcut;
 const
   FileProtocol = 'file:///';
 var
   ShortcutTitle : string;
 begin
   ShortcutTitle := Application.Title + '.URL';
 
   with TIniFile.Create(GetDesktopPath + ShortcutTitle) do
   try
     WriteString(
        'InternetShortcut',
        'URL',
        FileProtocol + Application.ExeName) ;
     WriteString(
        'InternetShortcut',
        'IconIndex',
        '0') ;
     WriteString(
        'InternetShortcut',
        'IconFile',
        Application.ExeName) ;
   finally
     Free;
   end;
 end; (*CreateSelfShortcut*)
 
Note: simply call "CreateSelfShortcut" to create a shortcut to your program on the Desktop.

.URL - When to Use?

I'm using those handy .URL files with my every project. When you create a setup for your applications, include an .URL shortcut inside the Start menu - let the users have the most convenient way to visit your web site for updates or examples or help files.
Related Video
Create Email Aliases and Forwarding Addresses in Gmail

©2014 About.com. All rights reserved.