How to run an application immediately after installation

It is quite common for commercial software to have a registration or configuration process which must be completed immediately after installation in order for the software to become operational. This blog entry outlines one technique for getting such a process to automatically occur after installation of a Windows Mobile software package.

My solution is largely based upon a sample application provided with the Windows Mobile 5.0 and Windows Mobile 6 SDKs. If you haven’t found these samples yet (installed by default) I encourage you to investigate them further.

Solution Overview
Most Windows Mobile Software is installed via CAB files. A CAB file is an archive which installs a number of files and registry settings onto a PDA. A less commonly used feature of a CAB file is that of a setup dll. A setup dll (traditionally named setup.dll) is a DLL which can hook into various steps of the installation/uninstallation process in order to extend or modify the process. The process of using a setup dll is described within MSDN in an article titled “Optional Setup.dll Files for installation“.

When the device processes a CAB file that includes a setup dll, it extracts the DLL and calls specifically named functions within it at the following stages of the installation process:

Since the DLL must export a set of functions it typically must be implemented in a native language such as C or C++. The functions exported by the setup.dll can perform any task they want and can optionally decide to abort the installation process.

So in order to run an application after installation of a CAB file, one approach is to write a setup dll that implements an Install_Exit() function that launches the newly installed executable.

Launching an application on installation
Since we want to perform a custom action in response to the installation process completing the correct place to place our custom code is the Install_Exit entry point.

One of the parameters to this entry point (as described in MSDN) is pszInstallDir which contains the path to the location the user has decided to install the application in (i.e. typically \Storage Card\Program Files\xyz or \Program Files\xyz). This will help us locate the executable that has been installed.

We can start the desired application by using the CreateProcess API. If we fail to launch the application we can return codeINSTALL_EXIT_UNINSTALL which will cause the CAB installation process to rollback any changes made during installation and display an error message to the user.

My sample application launches an application after installation by placing the following code within the Install_Exit function:

// We are provided with the installation folder the 
// user has installed the application into. So append
// the name of the application we want to launch. 
PROCESS_INFORMATION pi;
TCHAR szPath[MAX_PATH]; 
_tcscpy(szPath, pszInstallDir); 
_tcscat(szPath, _T("\\")); 
_tcscat(szPath, _T("TestApplication.exe")); 
 
// Start the application, and don't wait for it to exit 
if (!CreateProcess(szPath, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, &pi)) 
{ 
  // Failed to launch executable so indicate an error
  // and cause wceload to rollback the installation
  cie = codeINSTALL_EXIT_UNINSTALL; 
}

You will notice that this code sample hardcodes the name of the executable to launch (TestApplication.exe). It does however cope with users deciding to install the application on to an SD Card etc.

Using Visual Studio Smart Device CAB projects
ce-setup-dll-selected.pngce-setup-dll.pngHaving developed your custom setup.dll implementation you must package it into the CAB file in a particular way to indicate to the installation process that the file should be treated specially.

If you are using the Smart Device CAB project type within Visual Studio this is a two step process.

The first step is to add the “Primary Output” of your setup.dll project into the application folder of your setup project (use the file system viewer). This ensures that your dll is archived as part of the CAB file.

The second step is to indicate to the CAB building process which dll within your CAB file should be treated as the special “setup.dll”. To indicate which dll you want to use as the setup.dll within Solution Explorer you should select your Smart Device CAB project, and then switch to the Property viewer. You will notice one of the CAB file properties is called “CE Setup DLL”. Using a popup dialog that appears when this property is selected you should be able to navigate to the DLL you included in the first step of the process.

If you are creating your CAB file via a manually written INF file you specify the DLL that should be used by specifying a CESetupDLL parameter in your [DefaultInstall] section (documented within MSDN) as shown below:

[DefaultInstall]
CopyFiles = CopyToInstallDir,CopyToWindows
CESetupDLL = MySetup.dll

Sample Application

[Download sample application - 19.3Kb]

I have produced a small sample application which demonstrates this technique. It consists of a solution (SetupDLL.sln) that contains the following projects.

  • TestApplication – An application (written in C#) to run after the installation process.
  • SetupDLL – The DLL the CAB installation process invokes during installation in order to execute TestApplication.
  • SetupDLLCab – The CAB file project that packages everything up into a single CAB file for the end user to install.

If you build the example CAB file and then install it on a device you should notice that at the end of the installation process (just before the “installation has completed” message box appears) the included .NET Compact Framework application will automatically run.

I will end this blog posting with a question. Is anyone interested in being able to write such setup dlls within a managed language such as VB.NET or C#, and if so what kind of custom behaviour/logic would you be interested in implementing?

14 Responses to “How to run an application immediately after installation”

  1. MichaelP says:

    Hi there,

    Great post, thank you! I was wondering how to do this….

    Just one suggestion though, if I may:

    You may want to be careful with the usage of pszInstallDir. Whenever I want to append something to the end of a path that I received back from the system, I always do a check to ensure that I only add the “\” if necessary.

    For example, I’d probably add something like:

    _tcscpy(szPath, pszInstallDir);
    if (_T(’\\’) != pszInstallDir[_tcslen(pszInstallDir) - 1])
    {
    _tcscat(szPath, _T(”\\”));
    }
    _tcscat(szPath, _T(”TestApplication.exe”));

    [Disclaimer: I didn't test that code, so I'm not 100% sure if it works ;)]

    Otherwise, it’s possible to end up with a path with two back-slashes in it. In this particular case, if CreateProcess() fails with “invalid path,” your application will uninstall itself.

    In any case, again, great post!

  2. Im struggling to get this to work. Im not gonna ask the obvious silly question, what’s wrong, but I’m wondering if you know the steps involved to debug the installation dll? Im trying to attach from visual studio, but the code doesn’t seem to be loaded. Any idea?

  3. Lars Eriksson says:

    Great post!
    But when i open the solution there is something wrong with the SetupDLL, the output only sais (unable to determine name) and when i try to add projekt output for the setupDLL projekt its not found in the dropdownlist over projekts to add.

    i have also done the whole solution from scratch on my own and everything seems fine but when i install the cab nothing happends as if it dont even try to run the c++ code, i even dont get an exception just nothing.
    i have checked the inf-file that vs creates and the CESetupDLL is perfectly fine and the setup-file in the cab also seems totaly fine but still nothing happends.

    Any ideas?

  4. Hi Lars Eriksson,

    I am sorry to hear that you are having difficulty getting this code sample to build.

    I notice in the mean time that you have posted about your difficulties to the MSDN Forums website. I have replied with some suggestions to that post.

    Hope this helps,
    Christopher Fairbairn

  5. Grahame Armitage says:

    Thanks for pointing me in the right direction; I’ve managed to complete my assigned task of creating an installation CAB that runs another program after installation WITHOUT using MFC.

    Cheers,

    Grahame

  6. Mariano Alvarez says:

    How can I use this method to call another CAB file when the current CAB installer is done?

    Thanks,

  7. Hi Mariano,

    Please take a look at the Multiple CAB Install code sample that is available as part of the Windows Mobile Device SDKs.

    You essentially use the same technique discussed in this blog posting, but there is a small amount of extra complexity as cabwiz.exe (the executable responsible for installation of CAB files) does not allow multiple instances to run at the same time.

  8. Koush says:

    I created a Visual Studio template for CE Setup DLLs. Pretty useful.
    http://www.koushikdutta.com/2008/07/vc-project-template-for-smart-device-ce.html

  9. Mariano Alvarez says:

    Thanks Christopher. I was able to get it done following the instructions on the white paper. However, I notice that the main CAB does not delete the nested CABs once it’s done, which I don’t particulary like. In my case it left 2 CABs and the Multicab.exe file. Is it possible to have the process delete these files? I’m not sure how as Multicab is the one running it and it would have to delete itself? Not sure I’m being clear.

    Thanks!

  10. Ezhil says:

    Hi,

    How to run an application immediately after installation in managed code (using c#).

    Thanks

  11. Kevin says:

    Is there any way to do this sort of thing with C#?

  12. Tina says:

    Hi Christi,
    I have gone through your article and try to create the same CAB application on my own. It builds well but when I try to install it, it fails!
    I search for the error/mistake but I am not able to trace it. and I am using WM 6.1 professional as my device, does it giving me the problem?
    or it should work fine……………

    Thanks

  13. Udaya Kumar says:

    Hi Chris,

    Thanks for the great help. It worked. :). Now i have the answer to my client requirement.

  14. Zahoor says:

    Great Post…

    It helped me rebooting my Application at installation and removing few files at un-installation.

    Thanks.

Leave a Reply