The Essentials for Using COM in Managed Code
- Referencing a COM Component in Visual Studio .NET
- Referencing a COM Component Using Only the .NET Framework SDK
- Example: A Spoken Hello, World Using the Microsoft Speech API
- The Type Library Importer
- Using COM Objects in ASP.NET Pages
- An Introduction to Interop Marshaling
- Common Interactions with COM Objects
- Using ActiveX Controls in .NET Applications
- Deploying a .NET Application That Uses COM
- Example: Using Microsoft Word to Check Spelling
In This Chapter
Referencing a COM Component in Visual Studio .NET
Referencing a COM Component Using Only the .NET Framework SDK
Example: A Spoken Hello, World Using the Microsoft Speech API
The Type Library Importer
Using COM Objects in ASP.NET Pages
An Introduction to Interop Marshaling
Common Interactions with COM Objects
Using ActiveX Controls in .NET Applications
Deploying a .NET Application That Uses COM
Example: Using Microsoft Word to Check Spelling
So far we've covered the features of the .NET Framework, seen some managed code, and discussed how interoperating with COM works from a high level. This chapter shows what COM Interoperability means when sitting down at the keyboard and writing managed code that uses COM components. By the end of this chapter, you'll have all the knowledge you need to use many COM components in your .NET applications.
Before going into all the details on using COM components in .NET, let's see how Visual Studio .NET makes referencing a COM component fairly seamless. As mentioned in the previous chapter, one of the great benefits of COM access in the .NET Framework is that tons of COM components already exist that can fill in missing functionality in the pure .NET world. So, the first task in this chapter is to change the Hello, World programs of Chapter 1, "Introduction to the .NET Framework," into audible Hello, World programs using the Microsoft Speech API (SAPI) COM component. Of course, this requires a computer with speakers and the appropriate hardware. Also, if you don't already have it, download the Microsoft Speech Software Development Kit (SDK) version 5.1 from Microsoft's Web site and follow its instructions to install the software. Windows XP has the necessary software pre-installed.
Using COM components authored in Visual Basic 6 may crash during shutdown when used from a .NET application unless you have at least Service Pack 3 for the Visual Basic 6 runtime. If you have Visual Studio 6, you can download and install a Visual Studio 6 Service Pack. Otherwise, you can download Service Pack 5 for just the VB6 runtime at download.microsoft.com/download/vb60pro/Redist/sp5/WIN98Me/ EN-US/VBRun60sp5.exe. Windows 2000 and later operating systems already have a runtime with the necessary update.
Referencing a COM Component in Visual Studio .NET
Referencing a COM component is often the first step for using it in Visual Studio .NET, and it leads to the audible Hello, World sample later in this chapter. When referencing a COM component, you're actually referencing its type library, which provides the compiler with definitions of types, methods, and properties that the component exposes.
Type libraries can be standalone files with a .tlb extension, or they can be embedded in a file as a resource. They are typically contained in files with the extension .dll, .exe, .ocx, or .olb. A quick way to check for the presence of a type library in a file is to try opening the file in OLEVIEW.EXE, the utility introduced in the previous chapter.
Type libraries are often, but not always, registered in the Windows Registry. Among other things, this enables programs such as Visual Studio .NET to present a user with a list of type libraries on the current computer.
The steps for referencing a COM component's type library are:
Start Visual Studio .NET and create either a new Visual Basic .NET console application or a Visual C# console application. The remaining steps apply to either language.
Select the Project, Add Reference... menu. There are two kinds of components that can be referenced: .NET and COM. The default .NET tab shows a list of assemblies. To add a reference to a COM component, click the COM tab (see Figure 3.1). This tab should be familiar to Visual Basic 6 users, who reference a COM component's type library the same way (using the Project, References... menu in the Visual Basic 6 IDE).
The list of components consists of type libraries that are registered in the Windows Registry (under HKEY_CLASSES_ROOT\TypeLib). Each name displayed is the contents of the library's helpstring attribute, or the library name if none exists. To add a reference to a type library that isn't listed, click Browse... and select the file. This registers the type library, so the next time it is listed in the dialog. You should double-click Microsoft Speech Object Library in the list to try the upcoming example.
Click the OK button.
Figure 3.1 Adding a reference to a COM component using Visual Studio .NET, and the same task in Visual Basic 6.
It may surprise you to learn that the list of assemblies on the Add Reference dialog's .NET tab is obtained from the Windows Registry, not from the Global Assembly Cache. The GAC is not meant to be used at development time, so Visual Studio .NET uses the registry to find places in the file system to search for assemblies.
To add your own assemblies to the list, you can add a new subkey under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders key (or the same key under HKEY_CURRENT_USER) with any name, such as "My Company". Its default value should be set to the name of a directory, such as "C:\Program Files\My Company\". Any assemblies (with a .dll extension) that reside in this directory are loaded and listed with its assembly name, or the string inside its System.Reflection. AssemblyTitleAttribute custom attribute if it exists.
If adding the reference succeeded, a SpeechLib reference appears in the References section of the Solution Explorer window, shown in Figure 3.2.
Figure 3.2 The Solution Explorer after a reference has been added.
You can use the Object Browser (by clicking View, Other Windows, Object Browser) to view the contents of the SpeechLib library, as shown in Figure 3.3.
Figure 3.3 Browsing the type information for a COM