Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
COM has a long history within the Microsoft world. I remember working with COM and ActiveX back in the mid 1990s. At the time, it was a groundbreaking way to communicate between application components. Today, it looks antiquated and clumsy. But the Microsoft office applications continue to use COM and if you want to communicate with them, you will have to learn about this funky API. COM Interop is a powerful feature, giving you access to a lot of high-risk APIs, so naturally, you must be in an elevated trust app to make these COM calls.
Before showing you how to do this, however, I want to talk about how controversial this decision was to add COM support for the Silverlight team. The problem with adding support for COM is that Mac computers don't support it. So the dilemma facing the Silverlight team was this: Do we add COM support to Silverlight, which ignores the Mac portion of the Silverlight user community, or list to the requests of our corporate clients and find a COM Interop solution? They chose the latter, which is why I am going to show you how to do COM Interop. Let's move over to Visual Studio.
I'm in Visual Studio, and I have opened a project called GettingStartedWithComInterop. It has one file that I'm interested in looking at, which is the SpeechInterop.xaml file. Before I show you the code for this, I am going to be using the C# dynamic keyword, which is not available for a normal Silverlight application. So that means I need to add a reference to the Microsoft C# library. To do that, I go to my References folder, right-click, and choose Add Reference.
Then I sort my list alphabetically by clicking on Component Name, and then I scroll down till I find Microsoft.CSharp, Version 2, and click OK. That will add this node here, Microsoft.CSharp to my References folder. And now, in my code behind, over here in SpeechInterop.xaml.cs, I can use this keyword right here: private dynamic _speech. What does my code do? The first thing I do is I make a call to AutomationFactory.IsAvailable.
AutomationFactory is the way that we make our COM calls. IsAvailable checks for two things that are important for us to know if we are going to run a COM application. First, it checks that I am in an elevated trust application and secondly, it checks to see that I'm not running on a Mac. Next, I come down here and I create an instance of the SAPI.SpVoice. Now this in COM is called a ProgID, or program ID, and this represents the actual dll it's going to run in this speech code.
To learn more about this ProgID, I am going to go over to my Start menu and type in "regedit," which is the Registry Editor. And then I am going to choose Find > SAPI.spvoice. It searches through the registry and finds this section right here. There is the ProgID, and I'll click at the VersionIndependentProgID, SAPI.SpVoice.
And then if you look in the InprocServer32, there is the actual dll, the COM dll that I am going to call. So that's how it looks it up. It looks in the registry see using the ProgID, finds this physical dll, and makes that call. This line of code here in my Silverlight application is creating an instance of that Sp Voice. I am setting a property and I call it volume. I am formatting a spring to pass to that, and that has some indicators in how fast I want the voice to read back the script. And then I pass that string to the Speak method, and this is what it sounds like.
Let's press F5 to run it. Here's the text. I am going to click on the Read Text button. (Screen reader: Music news for today. Amazon announces new music price. 15 center per tune.) I'm also going to speed up the reading by moving the slider over to the right. (Screen reader: Hello world.) So there you go, simple and easy COM Interop. Obviously, there's more to COM Interop, and I'll show it to you in another movie in this chapter.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104241 Viewers
56 Video lessons · 116178 Viewers
71 Video lessons · 85386 Viewers
131 Video lessons · 40886 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.