Jeff Prosise's Blog

Silverlight 4's New COM Automation Support

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? :-)

On Dec 14 2009 6:47 AMBy jprosise With 15 Comments

Comments (15)

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

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


  3. 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);

  4. 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?

  5. 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?

  6. Hi Jeff

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

    Phil

  7. 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

  8. Qaiser Mehmood

    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

  9. Jeff,

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

    Luis
    Hazmat Software

  10. 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

Leave a Comment