Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 29 January 2010

Silverlight 4 : COM Interop..Endless Opportunities

Posted on 10:10 by Unknown

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?

Capture

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 }

Or

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

Or

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 (http://msdn.microsoft.com/en-us/library/dd264736(VS.100).aspx)

 

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;                                      
               _myspeech.Speak(textBox1.Text);
           }
           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 = "abc@abc.com";
_mydraftmail.Subject = "COM Interop Demo using Silverlight!";

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

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 !

Vikram.

Read More
Posted in | No comments
Newer Posts Older Posts Home
Subscribe to: Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ▼  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ▼  January (1)
      • Silverlight 4 : COM Interop..Endless Opportunities
  • ►  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ►  January (6)
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile