|
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:
Writing and Compiling the Managed CodeBefore 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 LibraryMost 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 AssemblyIn 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 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 CodeOnce 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.
|