Take a Shortcut

There are a number of ways you can make use of Shortcuts within your project. You may create a shortcut to your application during your CAB file installation – either on the Start Menu or perhaps in the WindowsStartup folder. VS2005 makes this easy because it has a nice file system graphical editor for CAB projects. Another way you can use shortcuts is within your code itself. There are two API functions to support this – SHCreateShortcut and SHGetShortcutTarget. By P/Invoking these from your .NETCF application you can create your own launcher applications which reuse the shortcuts setup for your Start Menu for example.

There are some caveats when using these functions though which you need to be aware of, the documentation has been revised since the Pocket PC 2003 SDK to warn you of this. To understand why let’s have a quick look at the contents of an LNK file:-

34#”My DocumentsTemplatesMemo.psw”


As you can see it’s a very simple text format. The number indicates the length of the path, this is followed by the # symbol then the full path to the target. When the path contains a space it must be wrapped in quotes. So what were those caveats? Well whatever path you pass to SHCreateShortcut it doesn’t add these quotes how you would expect – you end up with:-

34#”My” DocumentsTemplatesMemo.psw


This path is invalid and the shortcut won’t work. The solution is to wrap any path you pass into SHCreateShortcut with quotes to begin with. Even if the path doesn’t contain spaces this will give you a valid shortcut.

The second caveat comes with the SHGetShortcutTarget API. When this reads the shortcut file it doesn’t strip out the quotes if present, this means your returned path cannot be used as-is with any of the System.IO functionality. The solution to this one is very simple – perform a Trim() on the returned string passing in the ” character – this will remove the leading and trailing quotes and leave you with a valid path. If your shortcuts are to executables and also have arguments specified you will need to split the quoted path from any following arguments.

To demonstrate both of these techniques I’ve attached a sample VB.NET project. It can create and list shortcuts and when you tap on an item it will launch the target document. For a true launcher application there are some additional steps, for example retrieving the Icon for the file, these are not included in this sample. (7.27 KB)

By Peter Foot

Microsoft Windows Development MVP