• Microsoft LightSwitch – Open the System Calculator with Interop

    Posted Aug 21st, 2011 By in LightSwitch With| 8 Comments | Microsoft LightSwitch – Open the System Calculator with Interop
    Share on TwitterSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit

    The other day someone asked if they could open the Windows system calculator from LightSwitch. Because it is Silverlight based, I thought there might be a challenge, however it is actually quite easy to do. Here is how…

    I know that Microsoft Word can be accessed via some fancy Interop footwork, so I based my approach on that very same assumption that other stuff might be available via Interop. Here is how to get at the Windows Calculator via this interop magic…

    First, I created a simple LightSwitch project. No tables, just the project. Then, in the Solution Explorer I go all code monkey and select to view the solution in File View…

    Within the file view of the solution, I expand the Client project. I then right-click the Client project and select  to add a new class to the project…

    In my example, I named the class file MyCalculator.cs (yes, I am doing C# this time ;) ).

    Here is what the class file looks like…

    (C#)

    using System;
    using System.Net;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.Automation;
    
    namespace LightSwitchApplication
    {
        public class MyCalculator
        {
            public static void ShowCalculator()
            {
               dynamic calc = AutomationFactory.CreateObject("WScript.Shell");
               calc.Run(@"%SystemRoot%\system32\calc.exe");
            }
    
        }
    }

    (VB.Net)

    Imports System.Net
    Imports System.Text
    Imports System.Windows
    Imports System.Windows.Controls
    Imports System.Windows.Documents
    Imports System.Windows.Ink
    Imports System.Windows.Input
    Imports System.Windows.Media
    Imports System.Windows.Media.Animation
    Imports System.Windows.Shapes
    Imports System.Runtime.InteropServices
    Imports System.Runtime.InteropServices.Automation
    
    Namespace LightSwitchApplication
    	Public Class MyCalculator
    		Public Shared Sub ShowCalculator()
    			Dim calc As dynamic = AutomationFactory.CreateObject("WScript.Shell")
    			calc.Run("%SystemRoot%\system32\calc.exe")
    		End Sub
    
    	End Class
    End Namespace

    A couple of important thingies here. The first is the use of the System.Runtime.InteropServices namespace. It is via interop services that the magic will occur – which is what you can see being performed in the ShowCalculator method. Using the Windows Script Host object, the code simply runs a command line like call to fire up the calculator.

    That is all that is really required.

    Next, I go back to the solution explorer and select to view the solution in Logical View…

     In the logical view of the solution explorer, I right-click the Screens node and select to add a screen…

    In my example, I am only trying to figure out how to open a calculator, so forget about adding any tables or fancy screens. I just want to say, “Hey, that is cool, and I did it!”.

    So, in the Add New Screen dialog, I simply select to add a New Data Screen and click the OK button…

    With my new screen, all I need is some way to call that handy method in the MyCalculator class of the Client project. To do that, I’ll add a button to the screen and then call some code from that button. So, I click the Add Data Item button on the top of the screen designer, and then add a Method screen member with a name of ShowCalculator, and then click the OK button.

    In the screen designer, I drag and drop the ShowCalculator data item to the screen command bar…

    All this work has made me hungry at this point. I leave my home office desk and slather some yummy peanut butter and jam between two slices of bread, nibble away and wash it down with a nice cold glass of milk. Mmmmm milk!

    After cleaning the crumbs from my keyboard, I go back to the screen designer and right-click the newly added Show Calculator data item on the screen. In the resulting popup context menu, I click the Edit CanExecute Code item.

    In the resulting code window, I first add a using statement that gets me access to the InteropServices.Automation object… some

    using System.Runtime.InteropServices.Automation;

    This is important, because I need to first evaluate if in fact I can run this interop stuff. Remember, I am assuming that the application is running as a desktop application, and not a web application. So I update the ShowCalculator_CanExecute method with the following  incredibly time consuming code and complicated code…

    partial void ShowCalculator_CanExecute(ref bool result)
            {
                result = AutomationFactory.IsAvailable;
            }

    The above will tell the screen that, if interop is available, show the button. If not, don’t show it.

    Next I go back into the screen designer and just like I did with the CanExecute code, I select Edit Execute Code for the Show Calculator data item…

    In the code window, I update the ShowCalculator_Execute method with the following…

    partial void ShowCalculator_Execute()
            {
                MyCalculator.ShowCalculator();
            }

    Here is the entire code behind for the screen…

    (C#)

    using System;
    using System.Linq;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Collections.Generic;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Framework.Client;
    using Microsoft.LightSwitch.Presentation;
    using Microsoft.LightSwitch.Presentation.Extensions;
    using System.Runtime.InteropServices.Automation;
    
    namespace LightSwitchApplication
    {
        public partial class CreateNew
        {
            partial void ShowCalculator_CanExecute(ref bool result)
            {
                result = AutomationFactory.IsAvailable;
            }
    
            partial void ShowCalculator_Execute()
            {
                MyCalculator.ShowCalculator();
            }
    
        }
    }

    (VB.Net)

    Imports System.Linq
    Imports System.IO
    Imports System.IO.IsolatedStorage
    Imports System.Collections.Generic
    Imports Microsoft.LightSwitch
    Imports Microsoft.LightSwitch.Framework.Client
    Imports Microsoft.LightSwitch.Presentation
    Imports Microsoft.LightSwitch.Presentation.Extensions
    Imports System.Runtime.InteropServices.Automation
    
    Namespace LightSwitchApplication
    	Public Partial Class CreateNew
    		Private Partial Sub ShowCalculator_CanExecute(ByRef result As Boolean)
    			result = AutomationFactory.IsAvailable
    		End Sub
    
    		Private Partial Sub ShowCalculator_Execute()
    			MyCalculator.ShowCalculator()
    		End Sub
    
    	End Class
    End Namespace

    Now all that is left to do is run it and see what happens…

    SCORE!!

    That was easy. Now, if wanted to make it real interesting, I would add some nifty logic to Singleton the calculator, or maybe even do some User32.dll calls to handle some callbacks and get the value of the calculator for a field. That would be awesome… let me know when you do it  so I can copy what you did, okay?

    Cheers!


    • delicious
    • digg
    • reddit

    Paul
    My name is Paul Patterson and I am a software developer who has a keen interest in technology, including; open source, .Net, and anything Interweb. When not crafting some code, I can be found learning something new about photography. As well, I occasionally escape to the "music room" with my guitars to practice a few scales and then jam with my favourite FM radio stations.

Leave a Reply


Comments (8)

Reply
adefwebserver » 21. Aug, 2011

The sandwich was a nice touch. I think I will take a break and get something to eat also :)
It's nice you did this in Vb and C#

Reply
Ranjan » 23. Aug, 2011

Can I use this calculator inside a web application?

Reply
PaulSPatterson » 23. Aug, 2011

Hi Ranjan,

I don't believe you can use this calculator in a web application. The calculator is a Windows app. If you get it to work in a web app, let me know.

Cheers!

Reply
Ranjan » 23. Aug, 2011

Can you find a solution that how can we use this calculator or any silverlight calculator in a lightswitch web app.

Let me know

Reply
PaulSPatterson » 26. Aug, 2011

Take a look at this earlier article I did on creating and using a Silverlight control in LightSwitch. I am sure you can find, or create, a Silverlight calculator control.
http://www.paulspatterson.com/technology/lightswi

Reply
Ranjan » 23. Aug, 2011

Hello Paul

I Have also one code you can try this also

if (AutomationFactory.IsAvailable)
{
dynamic shell = AutomationFactory.CreateObject("Shell.Application");
shell.ShellExecute("calc.exe", "", "", "open", 1);
}
else
{
this.ShowMessageBox("Automation not available");
}

Reply
PaulSPatterson » 26. Aug, 2011

Excellent!

Thank you Ranjan,

Pauljavascript:void(0);

Reply
Windows Azure and Cloud Computing Posts for 10/6/2011+ - Windows Azure Blog » 06. Oct, 2011

[...] is based on a tip I saw from Paul Patterson that I thought was pretty clever: Open the System Calculator with Interop. This is a nice productivity feature for users working with numerical values on your screen. Just [...]

© Copyright Paul S Patterson - Please, no touchie. :)