How to avoid hardcoding file paths within CAB file registry settings

Screenshot showing Registry Editor window within Visual Studio

Sometimes when building a CAB file it is desirable to add registry values that refer to the location of files installed as part of the application. For example a document orientated application may configure file association registry entries that need to specify the location of the main executable.

The problem with hard-coding a registry value such as “\Program Files\MyApplication\MyApplication.exe” is that it breaks if the user chooses to install the application onto an SD Card, or the directory “\Program Files” is localised into some other name on a non English device. Ideally we would specify the registry value in such a way that it automatically adapts itself as part of the installation process. We can actually achieve this…

The Smart Device CAB project type within Visual Studio is a wrapper around the cabwiz.exe command line application. Behind the scenes the Visual Studio IDE takes all the settings you configure graphically, produces an *.INF file and passes it through cabwiz.exe. This means that the Visual Studio IDE supports many of the “power features” of the underlying tool.

One of these “power features” is the concept of Macro Strings. If you specify a registry value to be a string that contains the %InstallDir% macro the CAB installation process will automatically replace the %InstallDir% part of your string with the user’s specified installation directory.

As well as %InstallDir% there are a range of other macro strings which can be utilised as summarised in the table below:

Macro Substitution on English devices
%InstallDir% [directory application installed into]
%AppName% [name of application]
%CE1% \Program Files
%CE2% \Windows
%CE4% \Windows\StartUp
%CE5% \My Documents
%CE8% \Program Files\Games
%CE11% \Windows\Start Menu\Programs
%CE14% \Windows\Start Menu\Programs\Games
%CE15% \Windows\Fonts
%CE17% \Windows\Start Menu

Incidentally this feature is also the cause for a possible error message you may run across if you attempt to specify a registry value that contains an embedded % character…

“The Windows CE CAB Wizard encountered an error. See the output window for more information.”

Error: unsupported DirID 0
Error: File c:\xyz.inf contains DirIDs, which are not supported

This cryptic error message results from cabwiz.exe attempting to interpret the % character as a macro string. The solution to this problem is to escape single % characters with a second %. For example the value “100%” needs to be specified as “100%%”.

5 Responses to “How to avoid hardcoding file paths within CAB file registry settings”

  1. The string escaping trick is also required for strings which contain embedded quote mark (”) characters.

    In this case the IDE/cabwiz.exe is even more silent about the potential problem. If you specify a registry value that contains a single ” character it will silently be removed from the string.

    So

    test.exe “my app”

    turns into

    test.exe my app

    In order to get the string you want, you need to specify:

    test.exe “”my app”"

    Notice the double up of the quote marks?

  2. Raffaele Limosani has blogged about another limitation you might run up against while creating CAB files using Visual Studio that causes the same “xyz.inf contains DirIDs, which are not supported” error message.

    You can find the details and a suggested work around in his blog entry.

  3. Fahmi says:

    Hallo

    do you know how to set Root Directory for the Destination? I can’t find any macro Strings for Root Directory

    Thank you in advance
    Fahmi

  4. Hi Fahmi,

    The root directory of the device will always be \, so no macro is needed to safely cope with localised devices. You should be able to hardcode your path as \.

    Hope this helps,
    Christopher Fairbairn

  5. Anu says:

    Thanks a Lot. This was helpful !

Leave a Reply