Silverlight 4's New COM Automation Support

15 Comments December 14, 2009

One of Silverlight 4's most compelling new features is support for out-of-browser applications with elevated permissions. An app running with elevated permissions can perform actions that a normal sandboxed application can not. For example, it can access the local file system, and on Windows boxes, it can interact with COM automation servers. This latter feature—also new to Silverlight 4—is the subject of this blog post.

Silverlight 4's ComAutomationFactory class provides an API for instantiating COM automation objects and for determining whether COM automation is available. (It's not available if the app is running without elevated permissions, or if it's running on a Macintosh.) And C# 4.0's new dynamic keyword provides a means for interacting with automation objects, which by definition are late-bound (meaning they expose features to clients using an IDispatch interface).

One practical example of what you can do with Silverlight's COM automation support is sending an e-mail message via Outlook. The following sample does just that:

dynamic outlook = ComAutomationFactory.CreateObject("Outlook.Application"); 

dynamic mail = outlook.CreateItem(0);

mail.Recipients.Add("webmaster@contoso.com"); 

mail.Subject = "Hello, Silverlight";

mail.Body = "This message was sent from Silverlight 4"; 

mail.Save(); 

mail.Send();

The code is exceedingly simple and it works just fine if Outlook is installed on the client and the user OKs the prompt from Outlook warning that an external application is attempting to use it. For added robustness, you should catch the System.Exception thrown from ComAutomationFactory.CreateObject if object creation fails. That's exactly what will happen if Outlook isn't installed on the host PC.

For fun, I wrote a downloadable demo that uses Microsoft's speech automation server to verbalize error messages. (I definitely wouldn't recommend using this in a production application because it is quite annoying, but it's just novel enough to get a few laughs from your friends.) Here's how the application looks when it's running:

SpeechDemo Application 

If you click a button while the application is running inside the browser or outside the browser but without elevated permissions, the error message encapsulated in the ensuing exception is displayed in a message box. However, if you do the same with the application running with elevated permissions outside the browser, a friendly voice reads the error message out loud. Nothing calls attention to a null reference exception like a female voice informing you "Object reference not set to an instance of an object." For added fun, you could append "I'm sorry, Dave; I'm afraid I can't do that" to every error message.

When the application starts up, it attempts to instantiate the speech server and assign a reference to a local field:

if (ComAutomationFactory.IsAvailable)

{

    try

    {

         _speech = ComAutomationFactory.CreateObject("Sapi.SpVoice");

    }

    catch (Exception) { }

} 

Then, when an exception is thrown, it forwards the exception to a helper method named NotifyException, which verbalizes the error message if possible or displays it in a message box if not:

private void NotifyException(Exception ex)

{

    if (_speech != null)

        _speech.Speak(ex.Message);

    else

        MessageBox.Show(ex.Message);

}

If you install the application locally and OK its request for elevated permissions, you should hear (rather than see) the error messages. How's that for an error UI? :-)


15 Comments

  • Gravatar Image
    uberVU - social comments December 14, 2009 3:47 PM

    This post was mentioned on Twitter by jprosise: Blogged about the new COM automation support in Silverlight 4: http://tinyurl.com/y8f736a

  • Gravatar Image
    Saeed December 15, 2009 2:44 AM

    Great!
    dose SilverLight remain osIndependent in out-of-browser mode?
    if it dose, it would shape new generation in application development

  • Gravatar Image
    jprosise December 15, 2009 7:02 AM

    OOB apps work on the Mac, too. COM automation is Windows-only since the Mac doesn't support COM.

  • Gravatar Image
    Jeff Prosise's Blog December 16, 2009 4:43 PM

    In my last blog post , I wrote about Silverlight 4 applications that run outside the browser with elevated

  • Gravatar Image
    danipham January 21, 2010 12:20 PM


    How to attach an event with the same name as a method?

    For example in Word, Document.Close is a method and also an event.

    ((Word.DocumentEvents2_Event)document).Close += new Word.DocumentEvents2_CloseEventHandler(document_Close);

  • Gravatar Image
    Janka János szakmai blogja January 21, 2010 3:35 PM

    “One of the core tenets of the entire .NET architecture (compared to the native Win32 world) was security

  • Gravatar Image
    Nilay February 16, 2010 3:43 AM

    I am trying to add an image to power point from Silverlight, but not able to find any example where it illustrates how to add slide to Powerpoint and then add a picture to that using ComAutomationFactory of Silverlight 4. Can you please provide some direction here?

  • Gravatar Image
    Wolfgang March 1, 2010 8:25 AM

    Hey Jeff,

    I'm building a SL4-application which communicates with my business logic via IDispatch. This works fine in an out-of-browser scenario via installation.

    Now, to make things a bit more interesting, I'm trying to host Silverlight in a small ATL application. Now I want my elevation-heavy SL application to run in there, but I can find no way to give my native-hosted Silverlight plugin elevated trust.

    Do you have an idea?

  • Gravatar Image
    Phil Steel March 18, 2010 3:45 AM

    Hi Jeff

    Just to add the class has changed from ComAutomationFactory to AutomationFactory as of Silverlight 4 RC.

    Phil

  • Gravatar Image
    jprosise March 22, 2010 11:23 PM

    Yes, and the namespace name has changed, too.

  • Gravatar Image
    Cain May 13, 2010 8:35 PM

    Hi Jeff,

    Thanks for the post it was very useful, do the following conditions have to be true to allow a SL4 App to access a COM Visible DLL

    1. Running out of the browser

    2. Running with elevated permission

    3. On a windows machine

  • Gravatar Image
    Qaiser Mehmood May 26, 2010 6:22 AM

    Hi Jeff,
    I am looking for Com Automation Support for MS Access to read the tables from a local file, can u please give me some code hint about this.
    Thanks in advance.

    Qaiser Mehmood Mughal
    ---------------------------------
    CTO, The Experts Solution

  • Gravatar Image
    Luis July 6, 2010 5:56 PM

    Jeff,

    Can you write data to an MS Access database using the Silverlight 4 Com Automation Support?

    Luis
    Hazmat Software

  • Gravatar Image
    Vivek Gupta January 25, 2011 4:52 AM

    Can you please tell code on how to run code in elevated previlage in SL4

  • Gravatar Image
    George Birbilis November 8, 2012 11:31 AM

    There was a COM bridge for the Mac, or a cut-down version of OLE used by MS Office there I think. Not sure what they did with it. However they could have tried XPCOM or make an AppleScript bridge for COM or something for Macs

Have a Comment?

Archives

Tags