How to: Create COM Wrappers
You can create Component Object Model (COM) wrappers by using 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.
Wrap COM Objects in a Managed Application
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 or a
Using statement for C#.
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.
To create a runtime callable wrapper using .NET Framework tools
- Run the Tlbimp.exe (Type Library Importer) tool.
This tool creates an assembly that contains run-time metadata for the types defined in the original type library.
Wrap Managed Objects in a Native Application
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 parameterless 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 Regasm.exe (Assembly Registration Tool) 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 Gacutil.exe (Global Assembly Cache Tool) 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.