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:

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? 🙂

Jeff Prosise

View Comments

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

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

  • 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);

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

  • 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?

  • 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?

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

Recent Posts

8-Step AWS to Microsoft Azure Migration Strategy

Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…

5 days ago

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

2 weeks ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

4 weeks ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

1 month ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

1 month ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago