How to detect installed Silverlight version?


Recently, I wanted to detect the Silverlight version for one of my on going R&D. I wanted to detect the version from batch file and depending upon that I wanted to execute one file. Searching over internet I came to know that, HTML Application has some additional power for execution of any local file as it runs under fully trusted mode. I did more research on HTA files and successfully completed my basic analysis.

 

Here I will demonstrate you the process of detecting the Silverlight installation status using JavaScript from the .hta file. Read it and at the end, if you have any suggestion to improve the steps, please let me know.

 

What is HTA file?

Let us first discuss about the .hta file. What it is? HTA stands for HTml Application, by which you can run any script in fully trusted mode. You can read/write Registry values, you can do any file operation too. It is nothing but a HTML file. You can write HTML tags, JavaScripts, CSS Styles inside it as you do in HTML pages. But when you save it as a .hta file, the icon will change like an application icon. Then when you run the .hta file, it actually executes the file.

 

 

Detecting the Silverlight Version:

To detect the Silverlight version, you need to create the ActiveX Object of the AgControl using Scripting language like “JavaScript”. Once the object has been created, you can call the method IsVersionSupported() with proper parameter. This will return you the boolean value true/false. You must have to call the function in reverse chronological order of the parameter.

 

Have a look into the following JavaScript code to detect whether the Silverlight has been installed or not. If it is installed, it will return the latest Silverlight version.

 

<script type="text/javascript" language="javascript">
   1:  
   2:     function GetSilverlightVersion() {
   3:         // initialize the silverlightVersion to -1.
   4:         var silverlightVersion = -1;
   5:         getSilverlightVersion = function () {
   6:             try {
   7:                 // create the ActiveX Object of AgControl.
   8:                 // This is the core of Silverlight runtime.
   9:                 var control = new ActiveXObject('AgControl.AgControl');
  10:  
  11:                 // will execute if your latest Silverlight version is 4.
  12:                 if (control.IsVersionSupported("4.0")) {
  13:                     silverlightVersion = 4;
  14:                 }
  15:                 // will execute if your latest Silverlight version is 3.
  16:                 else if (control.IsVersionSupported("3.0")) {
  17:                     silverlightVersion = 3;
  18:                 }
  19:                 // will execute if your latest Silverlight version is 2.
  20:                 else if (control.IsVersionSupported("2.0")) {
  21:                     silverlightVersion = 2;
  22:                 }
  23:                 // if Silverlight version is not supported by your app,
  24:                 // set it as 0 (zero).
  25:                 else {
  26:                     silverlightVersion = 0;
  27:                 }
  28:                 control = null;
  29:             }
  30:             catch (e) {
  31:                 // if any exception while creating the ActiveX Object,
  32:                 // will set the silverlightVersion as -1.
  33:                 silverlightVersion = -1;
  34:                 alert("Unable to create the ActiveX Object from Browser window.");
  35:             }
  36:         }
  37:         // call to the inner function to detect the Silverlight.
  38:         getSilverlightVersion();
  39:  
  40:         // return the version of the Silverlight.
  41:         return silverlightVersion;
  42:     }
</script>

 

Here comes the power of .hta file. If you run the code inside the browser window, it will not be able to create the ActiveX object and will throw exception. To do this, you need to run the code in fully trusted mode and HTML application will do the tricks for you as it runs in full trust mode. You can write more condition inside it to check other versions.

 

 

Showing the Silverlight version:

Now, once you get the Silverlight version, what will you do? You need to show some message to the user. Create another script code. If the detected version comes as “–1”, there must be some error detecting the Silverlight plug-in.

 

If the detected version comes as “0” (zero) from the above JavaScript code, means the Silverlight plug-in is not installed in the PC. In the other case, it will print the desired major version to the screen.

 

Look into the code:

 

<script type="text/javascript">
   1:  
   2:     // get the Silverlight version
   3:     var silverlightVersion = GetSilverlightVersion();
   4:  
   5:     // if the retrived version is -1, means detection failed.
   6:     if (silverlightVersion == -1) {
   7:         document.writeln("Unable to detect the Silverlight Version.");
   8:     }
   9:     // if the retrived version is 0, means Silverlight is not installed.
  10:     else if (silverlightVersion == 0) {
  11:         document.writeln("Silverlight is not installed in your PC.");
  12:     }
  13:     // show the Silverlight version.
  14:     else {
  15:         document.writeln("You are using Silverlight " + silverlightVersion);
  16:     }
</script>

 

The above code is based on your requirement. If you want to show a message rendered into the screen, you will use the document.writeln() method. If you want to show an alert message to the user, you will use the alert() method from the JavaScript. You can do other operation too based on the business requirement.

 

Here is the full code for your reference:

 

<html>
<head>
    <title>Silverlight Version Tester</title>
    <script type="text/javascript" language="javascript">
   1:  
   2:         function GetSilverlightVersion() {
   3:             // initialize the silverlightVersion to -1.
   4:             var silverlightVersion = -1;
   5:             getSilverlightVersion = function () {
   6:                 try {
   7:                     // create the ActiveX Object of AgControl.
   8:                     // This is the core of Silverlight runtime.
   9:                     var control = new ActiveXObject('AgControl.AgControl');
  10:  
  11:                     // will execute if your latest Silverlight version is 4.
  12:                     if (control.IsVersionSupported("4.0")) {
  13:                         silverlightVersion = 4;
  14:                     }
  15:                     // will execute if your latest Silverlight version is 3.
  16:                     else if (control.IsVersionSupported("3.0")) {
  17:                         silverlightVersion = 3;
  18:                     }
  19:                     // will execute if your latest Silverlight version is 2.
  20:                     else if (control.IsVersionSupported("2.0")) {
  21:                         silverlightVersion = 2;
  22:                     }
  23:                     // if Silverlight version is not supported by your app,
  24:                     // set it as 0 (zero).
  25:                     else {
  26:                         silverlightVersion = 0;
  27:                     }
  28:                     control = null;
  29:                 }
  30:                 catch (e) {
  31:                     // if any exception while creating the ActiveX Object,
  32:                     // will set the silverlightVersion as -1.
  33:                     silverlightVersion = -1;
  34:                     alert("Unable to create the ActiveX Object from Browser window.");
  35:                 }
  36:             }
  37:             // call to the inner function to detect the Silverlight.
  38:             getSilverlightVersion();
  39:  
  40:             // return the version of the Silverlight.
  41:             return silverlightVersion;
  42:         }
  43:     
</script>
   1:  
   2: </head>
   3: <body>
   4:     <script type="text/javascript">
   5:         // get the Silverlight version
   6:         var silverlightVersion = GetSilverlightVersion();
   7:  
   8:         // if the retrived version is -1, means detection failed.
   9:         if (silverlightVersion == -1) {
  10:             document.writeln("Unable to detect the Silverlight Version.");
  11:         }
  12:         // if the retrived version is 0, means Silverlight is not installed.
  13:         else if (silverlightVersion == 0) {
  14:             document.writeln("Silverlight is not installed in your PC.");
  15:         }
  16:         // show the Silverlight version.
  17:         else {
  18:             document.writeln("You are using Silverlight " + silverlightVersion);
  19:         }
  20:     
</script>
</body>
</html>

 

Hope this will give you some basic idea and based on that, you will be able to improve the detection logic. Share your feedbacks/suggestions to improve this logic.

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

0 comments


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top