Creating a Standalone, offline Installer for installing Silverlight runtime + your XAP application

By Anoop Madhusudanan

Vote on HN

Related Downloads

What?

Recently a friend asked me to help him create an ‘installer’ to pack and distribute the Silverlight game he recently developed. He wanted to distribute his game via software download websites (like download.cnet.com), and via medias like CD/DVD.

In fact, that is pretty easy (technically) - and this post is about creating and deploying Silverlight Runtime + Your Silverlight application (Xap file) using your very own custom, standalone, offline installer with out getting a browser installed - instead of the user installing your Silverlight application Out Of Browser via the web browser.

Alright. In this post, we’ll see how to build a standalone installer for Silverlight Nerd Dinner Client that I built some time earlier (It is a Silverlight client for Scott’s Nerddinner.com to enable nerds to ‘eat in packs’). The installer will start with a Splash screen as well ;). 

image

About License related issues, see the Appendix towards the end of this post. Also, the scenario I’m discussing is limited to creating installers for Windows machines alone :).

Why?

I can already hear you thinking, “why this is required”? Think about this - You created a Nice game or a Cool gadget in Silverlight. Now you want to create a standalone Installer - so that you can push the same to your potential users via various means. Like, users may download your installer via CNET like software websites - or you may distribute your installer via CD/DVD media etc. In this way, you can push your Silverlight apps to the masses- instead of waiting for them to come to your website and discover it – You got it, that is what we are talking about :).

So, in short, I think Silverlight can be used a platform of choice for building light weight desktop applications, mainly applications like utility tools, light weight games etc.

I feel there are a good number of use cases where you want to do this.

  • You can write and deploy tiny apps and gadgets - with out worrying about the presence of/version of the .NET runtime in user’s machine.
  • You can maintain the same code base for your web and desktop versions if the scenario suits.
  • You can contribute towards increasing the number of boxes where Silverlight exists :). More people with Silverlight = a brighter web ;)

And yes, if you are doubtful about what you can really do from a Silverlight application that is running in an elevated trust, out of browser mode, check out this epic post from Justin Angel where he demonstrates how to do all whacky things with Silverlight + COM, including accessing Win 7 APIs, Accessing Scanners and printers, Adding your OOB application to windows startup etc. He rocks.

How?

So, let us see this in detail - how to create a stand alone installer to install Silverlight Runtime + Your Silverlight Application (i.e, the Xap file). These steps detail how I created my installer for Silverlight Nerd Dinner client.

1 – Preparing the required files

For this example, I’m going to use Nullsoft NSIS Installer to roll down the custom installer. NSIS is a simple windows installer from the WinAmp guys, and you can find ton of information here on that. You can write a custom NSIS script, and make a standalone installer executable out of that.

If you download the example package, you’ll find

  • The NSIS script to create the installer from.
  • The Silverlight runtime installer (Silverlight.exe) – You should ensure you have the latest from here.
  • Copy of your Silverlight Xap (that you are going to install in the user’s machine).
  • A license.txt file and splash.bmp file to show during the time of installation.

Download and Extract the Required Files zip - and have a look at the files I have. I’ve a Nerddinner.nsi script (we’ll soon see what is inside that), the Silverlight.exe runtime installer, SilverlightNerdDinner.xap, a license file and a Splash screen for the installer.

 

image 

2 – What the installer should do

Basically, we need our installer to

  • Copy Silverlight.exe to the user’s machine and execute that in Silent mode to install the Silverlight runtime.
  • Use Sllanucher.exe to install our Xap in out of the browser mode (Sllauncher.exe will be installed by the above Silverlight runtime installer) – Tim already has a nice post on using sllauncher.exe – I’ll also touch that a bit.

The Silverlight deployment guide is a handy read regarding the supported command line arguments for Silverlight.exe and Sllauncher.exe, but here is the summary.

You can install the Silverlight runtime in quiet mode, using the following arguments. 

Silverlight.exe /q /doNotRequireDRMPrompt

The '/q' switch will install the runtime in silent mode. Here is some excerpt from the Silverlight deployment manual version 4.0

  • /q = quiet install or upgrade. This installs or upgrades Silverlight without seeing the GUI. When Silverlight is installed quietly, by default privacy related features such as DRM protected media playback and the Silverlight auto-update feature will be configured to prompt the user for permission on 1st use of the respective features. The Silverlight auto-update feature requires administrative rights so non-admin users will not be prompted.
  • /doNotRequireDRMPrompt = turns off the 1st use prompt allowing content protected by Digital Rights Management (DRM) to play without requiring any end-user intervention. When Silverlight is installed quietly, DRM Playback is set to prompt on 1st use by default.
  • /ignorewarnings = non-fatal warnings will not be reflected in the quiet installer return code but will instead return zero indicating success.
  • /noupdate = disables the Silverlight internal auto-updater.
  • /qu = quiet uninstall. This uninstalls Silverlight without seeing the GUI.

The Silverlight runtime installer will install sllauncher.exe to the {PROGRAMFILES}\Microsoft Silverlight\sllauncher.exe path. So, after installing Silverlight runtime, we may use sllauncher.exe to ‘install’ our xap in Out of Browser mode, and to create the required short cuts

sllauncher.exe 
           /install:"{LocalPathToXapFile}\Yourfile.xap" 
           /origin:"{urltoxapfile}/Yourfile.xap" 
           /shortcut:desktop+startmenu

You can see that Sllauncher.exe will accept the local path to the xap file, the url to the xap file and the shortcut options. Here is some excerpt from the Silverlight deployment manual about using sllauncher.exe to install XAP.

Installation

  • /install:"xapFile" – where xapFile is the file name/file path to the .xap file., E.g. /install:"c:\temp\sample.xap"
  • /origin:"xapURI" – where xapURI is the URI where the .xap file would've come from if not installed at the commandline
    • e.g. /origin:"http://example.com/sample.xap" .  This URI will be used as the site of origin for security purposes. For example, for sandboxed applications, Silverlight networking requires a policy file check when making network requests to domains other than the site of origin.  The origin also serves as a unique ID for the application.  The xapURI must be an absolute URI not a relative URI, and must start with http:, https:, or file:.
  • /overwrite -- (optional) Overwrites any previously installed .xap with that xapURI.

Un installation

  • /uninstall  – Uninstalls the application specified in /origin. This is the same /origin value that was used to install the app originally.
  • /origin:"xapURI" – same as /origin for the install case

3 – The NSIS Script

So, now let us write a quick NSIS script to create an installer that performs these operations. Our script will look like this

Name "Nerd Dinner Desktop Installer"
OutFile "NerdDinnerDesktopInstaller.exe"
InstallDir "$PROGRAMFILES\NerdDinnerDesktop"

XPStyle on

Function .onInit
	# the plugins dir is automatically deleted when the installer exits
	InitPluginsDir
	File /oname=$PLUGINSDIR\splash.bmp "splash.bmp"
	splash::show 1000 $PLUGINSDIR\splash
	Pop $0
FunctionEnd

Section
    SetOutPath "$INSTDIR"
    SetOverwrite ifnewer
    File "Silverlight.exe"
    ExecWait "$INSTDIR\Silverlight.exe /q /doNotRequireDRMPrompt"
    File "SilverlightNerdDinner.xap"
    ExecWait '"$PROGRAMFILES\Microsoft Silverlight\sllauncher.exe" /install:"$INSTDIR\SilverlightNerdDinner.xap" /origin:"http://amazedsaint.net/nerddinner/clientbin/silverlightnerddinner2.xap" /shortcut:desktop+startmenu'
SectionEnd

Basically, the installer will

  1. Show a splash screen during initialization of the installer
  2. Copy Silverlight.exe to the target folder, and execute it in quite mode
  3. Copy our xap file to the target folder, and use sllauncher.exe to install the xap

4 – Creating the Installer

Once you have NSIS installed, you can simply right click on an NSIS file to ‘Compile’ it create an installer.

image

You should see the MakeNSISW prompt, with a message that the installer is created. Our final installer (that contains Silverlight.exe runtime + XAP + Splash Screen image) is almost ~7 MB, and now you can distribute this the way you need, provided you have the permission from Microsoft to re-distribute Silverlight runtime.

image

The above installer will install your Silverlight application and will create the desktop and start menu short cuts. Try the installer from here, and you’ll see that it’ll get installed even in a machine with no Silverlight runtime already present.

When you create the installer from your own XAP file, make sure you have enabled Running your Silverlight Application out of browser. Also, always make sure you’ve the latest version of Silverlight runtime installer, Silverlight.exe (Normally what ever you get from this link )

 

Appendix - Licensing Issues

I asked Tim and Scott whether I can redistribute Silverlight runtime as part of my own custom installer. And this is the reply I got. There is a license available, but it depends on the situation. You should send your app details to Tim for the details :) Also, you might want to show the Silverlight license to the end users as part of your installer. I’m not pretty sure about this – the general recommendation is to fled Tim’s inbox.

image

Having said that, I really think MS should encourage developers to re-distribute Silverlight runtime as part of their custom installers, because that can definitely contribute towards increasing the Silverlight runtime penetration.

Shout it
© 2012. All Rights Reserved. Amazedsaint.com