|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Create COM Wrappers
You can create Component Object Model (COM) wrappers by using Microsoft Visual Studio 2005 features or the .NET Framework tools Tlbimp.exe and Regasm.exe. Both methods generate two types of COM wrappers:
A Runtime Callable Wrapper from a type library to run a COM object in managed code.
A COM Callable Wrapper with the required registry settings to run a managed object in a native application.
In Visual Studio 2005, you can add the COM wrapper as a reference to your project.
To create a runtime callable wrapper using Visual Studio
Open the project for your managed application.
On the Project menu, click Show All Files.
On the Project menu, click Add Reference.
In the Add Reference dialog box, click the COM tab, select the component you want to use, and click OK.
In Solution Explorer, note that the COM component is added to the References folder in your project.
You can now write code to access the COM object. You can begin by declaring the object, such as with an Imports statement for Visual Basic 2005 or a Using statement for Visual C# 2005.
If you want to program Microsoft Office components, first install the Microsoft Office Primary Interop Assemblies (PIAs) from the Microsoft Download Center. In step 4, select the latest version of the object library available for the Office product you want, such as the Microsoft Word 11.0 Object Library. For samples, see Microsoft Word Technology Sample and Microsoft Excel Technology Sample.
To create a runtime callable wrapper using .NET Framework tools
Run the Type Library Importer (Tlbimp.exe) tool.
This tool creates an assembly that contains run-time metadata for the types defined in the original type library.
To create a COM callable wrapper using Visual Studio
Create a Class Library project for the managed class that you want to run in native code. The class must have a default constructor.
Verify that you have a complete four-part version number for your assembly in the AssemblyInfo file. This number is required for maintaining versioning in the Windows registry. For more information about version numbers, see Assembly Versioning.
On the Project menu, click Properties.
Click the Compile tab.
Select the Register for COM interop check box.
When you build the project, the assembly is automatically registered for COM interop. If you are building a native application in Visual Studio 2005, you can use the assembly by clicking Add Reference on the Project menu.
To create a COM callable wrapper using .NET Framework tools
Run the Assembly Registration Tool (Regasm.exe) tool.
This tool reads the assembly metadata and adds the necessary entries to the registry. As a result, COM clients can create .NET Framework classes transparently. You can use the assembly as if it were a native COM class.
You can run Regasm.exe on an assembly located in any directory, and then run the Global Assembly Cache Tool (Gacutil.exe) to move it to the global assembly cache. Moving the assembly does not invalidate location registry entries, because the global assembly cache is always examined if the assembly is not found elsewhere.