Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Build a .NET Client That Uses a COM Server?

This section explains how to build managed code that uses COM. The steps involved in the build process are as follows:

  1. Obtain an assembly containing definitions of the COM types to be used.
  2. Install the assembly in the global assembly cache. (optional)
  3. Reference the assembly containing the COM type definitions.
  4. Reference the COM types.
Obtain an Assembly Containing Definitions of the COM Types to be Used
Before any managed application can reference a specific type, the type must be described in metadata. For managed types, this is easy because the compiler that produces the managed code also produces the necessary metadata. Getting metadata for existing COM types is a little trickier. There are several ways to do this.
  1. Obtain a signed assembly containing the metadata from the producer of the library. The provider of the COM types should be the first source of metadata for any COM library. This allows the vendor to sign the assembly with the correct publisher key.

  2. If a signed assembly is not available from the publisher, consider using the tlbimp.exe (Type Library Importer) utility to produce an assembly yourself. You can produce an assembly from most COM type libraries. If the assembly is to be shared, the assembly must be signed with a publisher's key. The tlbimp.exe can produce signed assemblies using the /keyfile option.

    tlbimp SHDocVw.dll /keyfile:MyKey.snk /out:ExplorerLib.dll
  3. As a last resort, the metadata can be created directly from source code. In order to do this, the types are defined in a language like C# or Visual Basic and marked with the ComImportAttribute. The attribute is used to by the runtime to differentiate these types from other managed types. There are several other attributes that can be used to customize how a managed type is exposed to COM.

    [ComImport] interface IMyComInterface {...}
Install the Assembly in the Global Assembly Cache
If you want your assembly containing definitions of the COM types to be shared among several applications, it must be installed in the global assembly cache (GAC). Use gacutil.exe to install an assembly in the GAC.

gacutil /i ExplorerLib.dll
Reference the Assembly Containing the Type Definitions.
With Visual Basic or with C#, you can reference the assembly using the compiler /r switch or you can add reference to the project directly from Visual Studio development tool.

csc TestClient.cs /r:ExplorerLib.dll

Reference the COM Types
Once the reference to the type library is added to the project, the types defined within that library can be referenced from managed code. Refer to the How Do I...Call COM methods from .NET? for an example on how to do that.

Note: See the article on http://support.microsoft.com/support/misc/kblookup.asp?id=Q264957 when using VB6 components from managed code.

Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.