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([email protected]); 

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