- 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
- Conclusion
Referencing a COM Component Using Only the .NET Framework SDK
Now, let's look at how to accomplish the same task using only the .NET Framework SDK. The following example uses the .NET Framework Type Library to Assembly Converter (TLBIMP.EXE). This utility is usually just called the type library importer, which is what TLBIMP stands for. As mentioned in Chapter 1, if you've installed Visual Studio .NET but still want to run the SDK tools, you may have to open a Visual Studio .NET command prompt to get the tools in your path.
The steps for referencing a COM component without Visual Studio .NET are:
From a command prompt, run TLBIMP.EXE on the file containing the desired type library, as follows (replacing the path with the location of SAPI.DLL on your computer):
TlbImp "C:\Program Files\Common Files\Microsoft Shared\Speech\sapi.dll"
This example command produces an assembly in the current directory called SpeechLib.dll because SpeechLib is the name of the input library (which can be seen by opening SAPI.DLL using the OLEVIEW.EXE utility). A lot of warnings are produced when running TLBIMP.EXE, but you can ignore them.
Reference the assembly just as you would any other assembly, which depends on the language. Using the command-line compilers that come with the SDK, referencing an assembly is done as follows:
C# (from a command prompt):
csc HelloWorld.cs /r:SpeechLib.dll
Visual Basic .NET (from a command prompt):
vbc HelloWorld.vb /r:SpeechLib.dll
Visual C++ .NET (in source code):
#using <SpeechLib.dll>
After TLBIMP.EXE has generated the assembly, you can browse its metadata using the IL Disassembler (ILDASM.EXE) by typing the following:
ildasm SpeechLib.dll
The name IL Disassembler is a little misleading because it's really useful for browsing metadata, which is separate from the MSIL. As shown in Figure 3.4, most SpeechLib methods don't even contain MSIL because the methods are empty implementations that forward calls to the original COM component. This can be seen by double-clicking on member names.
Figure 3.4 Using the IL Disassembler as an object browser.
ILDASM.EXE gives you much more information than the object browser in Visual Studio .NET, including pseudo-custom attributes. The information in the windows that appear when you double-click on items is explained in detail in Chapter 7, "Modifying Interop Assemblies."