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 COM Server Callable from COM clients?

This section explains how to build and install managed code that will be used from COM applications. The steps involved in the build process are as follows:

  1. Write and compile the managed code.
  2. Generate a type library for the assembly using the tlbexp.exe utility.
  3. Install and register the assembly.
  4. Write and compile the COM code that references types in the assembly.
Writing and Compiling the Managed Code
Before setting out to build an assembly that will be used from COM, it is important to understand the limitations of the common language runtime's interoperability services. Refer to the Get Started with Interoperability for specific details on exactly what those limitations are.

If your managed assembly is designed to be shared among several applications, be sure that the assembly has a strong name so that it can be installed in the global assembly cache (see How Do I...Create an Assembly with a Strong Name?). If your assembly does not have a strong name, it can only be used by a single application.

Once the managed code is written, the compilation process is the same as it would be for any other piece of managed code.

Generating a Type Library
Most unmanaged application development tools require a type library before you can make references to external types. A type library can be generated from an assembly using the tlbexp.exe, which produces a .tlb file that can then be referenced from your unmanaged development tool.
tlbexp TestServer.dll 

For example, with Visual Basic 6.0, you can reference the .tlb file from the Project/References dialog. In Visual C++ 6.0, you can use the #import statement to import the type definitions from the type library directly into C++. Once the reference to the type library is added to the project, the types defined within that library can be referenced from unmanaged code.

Installing and Registering the Assembly
In order to actually create managed types from unmanaged code, the assembly needs to be installed in the global assembly cache (GAC) and registered for use from COM.

You can install an assembly in the global assembly cache using gacutil.exe utility. Assemblies can be uninstalled using the /u option.

gacutil /i TestServer.dll 
If the assembly is to be used privately by a single application, it can be copied directly to the application directory (rather than installing the assembly in the GAC). Private assemblies do not require strong names, but they must be registered for use from COM.

You can register an assembly using regasm.exe utility. Assemblies can be uninstalled using the /u option.

regasm TestServer.dll 

Writing and Compiling the Unmanaged Code
Once the assembly is registered and properly installed, the types defined within the assembly can be used from COM as though they were normal COM types. For example, new objects can be created by calling CoCreateInstance API or by calling CreateObject or GetObject from Visual Basic or script languages. See the remaining samples in this section for specific coding details.




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