Friday, January 29, 2010

Silverlight 4 : COM Interop..Endless Opportunities

These days offline sessions keeping me very busy and because of that I am not able to come here frequently.Well, Today I am going bit ahead and will be talking about COM Interop capabilities with version 4 Beta with some small examples and few lines of code. Purpose of this article is to make you aware of COM Interop feature and how you can empower your Silverlight applications with this.

Why you need COM Interop?

We all know the limitations of each technology and upto what level customization and integration can be done. Sometimes it becomes difficult to address all business scenarios using one single technology, for something like this, COM Interop is case here. Its all about communicating among apps and making life more simple and real !

Power of Silverlight !

There are endless discussion going on about Silverlight and Flash,their comparison and future and lifespan too especially HTML 5 is somewhere around the corner. Well as a Silverlight MVP I can only tell you that..Silverlight is full of potential,innovation,creativity and ideas and future is Silverlight :)

Silverlight with COM Interop now gives you a highway where your Silverlight application can talk to other applications doing some or different function.

Have you ever imagined that your Silverlight Application is talking to MS Office? sounds crazy ? then look at few snippets below, how Silverlight 4 makes it very easy for you.

What you need for COM Interop in Silverlight ?

  • COM ( Necessary component should be present on system. e.g Speech SDK/API)
  • ComAutomationFactory API
  • System.Windows.Interop Namespace
  • “dynamic” keyword from C# 4.0
  • Handy COM Documentation

Anything else?


So, you have to set it via OOB settings, then only it is possible to execute since it runs with elevated trust aka elevated permissions/trusted application.

Any Problems I may face?

Well, did I tell you that “IntelliSense” will not work here in COM Interop? Shocked?..even I was first time. With ComAutomationFactory it allows you to play with COM but it will not give you all stuff like Members,Methods etc inside IntelliSense for a simple reason is since it falls in outside world. So we are helpless and should give up??

No, Documentation is always there ! (That’s what we Developer guys spend most of the time in our Life !), you need to refer documentation with that COM so as to get aware yourself about the various functional aspects of that COM.

How about checking those Trust and Permissions thing ?

A simple logic can be used like this so as to ensure it running under safe arena, like this :

if (App.Current.HasElevatedPermissions)
{ // Your code here }


if (ComAutomationFactory.IsAvailable)
{ //Your code here }


if (App.Current.InstallState == InstallState.Installed)
{ //Your code here }

These are the smart ways to ensure.

What is that “dynamic” keyword do?

If you are looking at definition of “dynamic” then it is like this :


Represents an object whose operations will be resolved at runtime

Via MSDN (


Visual C# 2010 introduces a new type, dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object. At compile time, an element that is typed as dynamic is assumed to support any operation. Therefore, you do not have to be concerned about whether the object gets its value from a COM API, from a dynamic language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or from somewhere else in the program. However, if the code is not valid, errors are caught at run time.

Any simple example for dummies like me?

Well, There are tons of example over net, I will show small snippets of 1 or 2 for Speech API and MS Word and Outlook interaction.

1. Text to Speech :

using System.Windows.Interop;

private void button1_Click(object sender, RoutedEventArgs e)
           if (App.Current.HasElevatedPermissions)
               dynamic _myspeech = ComAutomationFactory.CreateObject("Sapi.SpVoice");
               _myspeech.Volume = 100;                                      
           else MessageBox.Show("Kindly install the application first and then try again !");

2. Sending Text from Textbox to MS Word :

using System.Windows.Interop;

dynamic _myword = ComAutomationFactory.CreateObject("Word.Application");
            _myword .Visible = true;
            dynamic _mydoc = _myword .Documents.Add();

            string Insertxt = textBox1.Text;
            dynamic range = _mydoc.Range(0, 0);

            range.Text = Insertxt;

3. Interact with Outlook :

dynamic _myoutlook = ComAutomationFactory.CreateObject("Outlook.Application");
dynamic _mydraftmail = _myoutlook.CreateItem(0);

_mydraftmail.To = "";
_mydraftmail.Subject = "COM Interop Demo using Silverlight!";

_mydraftmail.HTMLBody = "<P>My First Mail from COM Interop</P>";

I guess 3 example above are more than sufficient to showcase the richness of interaction of Silverlight with COM, Do I need to comment on where these can be used?..I know COM Interop with Silverlight brings endless ideas and opportunities..that’s what my title of today’s post.

Don’t forget to drop me feedback via example 3 :)

South Asia MVP Open Day 2010 last week at Microsoft India in Hyderabad, My job and overtime on weekends and Silverlight Sessions in User Groups kept me busy for a long and away from Silverlight and you, but now, things are normal and soon I will be hitting few more features of Silverlight 4 very soon.

Again, Focus will be on pushing concepts and not code..So get cup of coffee and come back here for more !