Packaging an Assembly for COM

COM developers can benefit from the following information about the managed types they plan to incorporate in their application:

  • A list of types that COM applications can consume

    Some managed types are invisible to COM; some are visible but not creatable; and some are both visible and creatable. An assembly can comprise any combination of invisible, visible, not creatable, and creatable types. For completeness, identify the types in an assembly that you intend to expose to COM, especially when those types are a subset of the types exposed to the .NET Framework.

    For additional information, see Qualifying .NET Types for Interoperation.

  • Versioning instructions

    Managed classes that implement the class interface (a COM interop-generated interface) are subject to versioning restrictions.

    For guidelines on using the class interface, see Introducing the Class Interface.

  • Deployment instructions

    Strong-named assemblies that are signed by a publisher can be installed into the global assembly cache. Unsigned assemblies must be installed on the user's machine as private assemblies.

    For additional information, see Assembly Security Considerations.

  • Type library inclusion

    Most types require a type library when consumed by a COM application. You can generate a type library or have COM developers perform this task. The .NET Framework SDK provides the following options for generating a type library:

    • Type Library Exporter
    • TypeLibConverter Class
    • Assembly Registration Tool
    • .NET Services Installation Tool

    Regardless of the mechanism you choose, only public types defined in the assembly you supply are included in the generated type library.

You can package a type library as a separate file or embed it as Win32 resource file within a .NET-based application. Microsoft Visual Basic 6.0 performed this task for you automatically; however, when using Microsoft Visual Basic .NET, you must embed your type library manually.

To embed a type library as a Win32 resource in a .NET-based application

  1. Compile your managed application as a library file. For example, to create an assembly for an application called MyApp, type the following command at the command prompt:

    vbc /t:library MyApp.vb
    [C#]
    csc /t:library MyApp.cs
    
  2. Export a type library file from your assembly using Tlbexp.exe. At the command prompt, type the following command:

    tlbexp MyApp.dll /out:mytypelib.tlb
    
  3. Create a resource script that contains the following statement:

    IDR_TYPELIB1 typelib "mytypelib.tlb"
    

    For this example, the script file name is myresource.rc.

  4. Compile the script using the Microsoft Windows Resource Compiler (Rc.exe). At the command prompt, type the following command:

    rc myresource.rc
    

    Rc.exe produces the myresource.res resource file.

  5. Compile the source file again and specify the resource file. At the command prompt, type the following command:

    vbc /t:library MyApp.vb /win32res:myresource.res
    [C#]
    csc /t:library MyApp.cs /win32res:myresource.res
    

Type Library Exporter

The Type Library Exporter (Tlbexp.exe) is a command-line tool that converts the classes and interfaces contained in an assembly to a COM type library. Once the type information of the class is available, COM clients can create an instance of the .NET class and call the methods of the instance, just as if it were a COM object. Tlbexp.exe converts an entire assembly at one time. You cannot use Tlbexp.exe to generate type information for a subset of the types defined in an assembly.

TypeLibConverter Class

The TypeLibConverter class, located in the System.Runtime.Interop namespace, converts the classes and interfaces contained in an assembly to a COM type library. This API produces the same type information as the Type Library Exporter, described in the previous section.

The TypeLibConverter class implements the ITypeLibConverter Interface.

Assembly Registration Tool

The Assembly Registration Tool (Regasm.exe) can generate and register a type library when you apply the /tlb: option. COM clients require that type libraries be installed in the Windows registry. Without this option, Regasm.exe only registers the types in an assembly, not the type library. Registering the types in an assembly and registering the type library are distinct activities.

.NET Services Installation Tool

The .NET Services Installation Tool (Regsvcs.exe) adds managed classes to Windows 2000 Component Services and combines several tasks within a single tool. In addition to loading and registering an assembly, Regsvcs.exe can generate, register, and install the type library into an existing COM+ 1.0 application.

See Also

Exposing .NET Framework Components to COM | Qualifying .NET Types for Interoperation | Introducing the Class Interface | Assemblies and Security Considerations | Type Library Exporter (TlbExp.exe) | TypeLibConverter Class | ITypeLibConverter Interface | Registering Assemblies with COM