Monday, December 22, 2008

Interoperating with COM - Using COM from the .NET Framework (1)

How to Add a Reference to a COM Library or Type Library

COM libraries contain classes, methods, and types.
COM types are defined in a type library which can be a stand-alone .tlb file or can be embedded in a .dll, .exe, .ocx, or .olb file.

The easiest way to access a COM library or type library file in . NET framework application is to add a reference to it from Visual Studio, and then you can use the methods in it.
  1. Open your project or create a new project in Visual Studio.
  2. Go to "Project" menu, click "Add Reference". (Or right-click "References" folder of your project, and select "Add Reference".)
  3. Click "COM" tab, and select a COM component from the list
  4. Or you can click "Browse" tab to select a file. Then click OK.
After you add a reference to the COM component, Visual Studio will automatically create a assembly version of that COM file with Interop.*.dll name. For example, if you add MSO9.dll (Microsoft Office 9.0 Object Library) to the reference, VS will create a Interop.Office.dll, while "Office" is the namespace of the COM file.

Moreover, you can see the new reference in your References folder of your project. You can double-click it to open it in Object Browser to see its namespace, methods, classes...etc.

No comments:

Post a Comment