Silverlight 4.0 : Open .Exe Files using COM Interoperability

While working with Silverlight Line of Business applications, there may be a scenario where the user wants to open .exe applications e.g. Notepad or Calculator from the local machine. The question here is how to make this possible in Silverlight?

In Silverlight 4.0, using the AutomationFactory class we can create an instance of these .exe applications and can use them along with our Silverlight application. CreateObject is the method provided for such type of application development. To make use of this method, you need to add a reference of the .CSharp library in your Silverlight application. This library file is available in the following path on my machine:

C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\Microsoft.CSharp.dll

Step 1: Open VS 2010 and create a new Silverlight application targeted to use Silverlight 4.0. Name the application as ‘SL4_Running_System_Executables’.

Step 2: Right click on the project and select properties. Check the ‘Enable running application out of browser’ checkbox as shown below:

Out of Browser

Step 3: Also click on the ‘Out-of-Browser Settings’ button and check the ‘Require elevated trust when running outside the browser’ checkbox as shown below. This will allow the Silverlight application running out of the browser to interact with the system libraries and applications.

Elevated Trust

Step 4: In the MainPage.Xaml, add two buttons as shown below:

Silverlight UI

Step 5: Use the following namespace in the MainPage.xaml.cs file

using System.Runtime.InteropServices.Automation;

Step 6: Write the following code in the Click events for both the Buttons:

Silverlight open exe

The above code makes use of the WScript object which provides access to the root object for the Windows Script Host object model. The WScript.shell provides an object of native Windows shell. You can use this object to make call to .exe’s and libraries from the local system.

Step 7: Run the application and test it by clicking on the buttons as shown below:

image

The business requirements here is that the end-user may want to make note of some data before passing it to the Silverlight application. In such cases, Notepad can be a lightweight program.

Similarly in some cases, the user may want to perform calculations like percentage before entering into a system. In such cases, the Calculator can be an ideal application.






About The Author

Mahesh Sabnis is a Microsoft MVP having over 18 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). He also blogs regularly at DotNetCurry.com. Follow him on twitter @maheshdotnet

3 comments:

Anonymous said...

please tell me how to do it in VB ??

Anonymous said...

thank you! it was very helpful but can u please tell how we can run it from IIS.

123 said...

10x a lot