CsCom
COM interop for .NET that works with trimming and Native AOT.
CsCom produces COM interop code from type libraries or existing .NET interop assemblies, similar to TlbImp. Unlike TlbImp, it does not rely on the built-in COM interop of .NET: it generates C# code that uses the source-generated COM interop added in .NET 8, so the interop code can be trimmed and used with Native AOT. It also goes the other way, like TlbExp: it exports the type library of an assembly whose COM types are declared for source-generated COM interop.
IDictionary dictionary = Dictionary.Create(); // a COM object of scrrun.dll
dictionary.CompareMode = CompareMethod.TextCompare; // properties are C# 14 extension properties
dictionary.Add("apple", 3); // VARIANT parameters are object?
foreach (object? key in dictionary) // _NewEnum, through IEnumVARIANT
{
Console.WriteLine(key);
}
Get started · Migrate from TlbImp · API reference · GitHub
Three ways to use it
- During the build: the CsCom.Build package generates the interop code of the type libraries
that a project lists as
CsComReferenceitems, before the compilation. - As a project:
cscom project createwrites a C# project with the interop code, whichcscom project updateregenerates, see Commands. - As an assembly or a package:
cscom assembly buildandcscom assembly packbuild a ready-to-use interop assembly or NuGet package.
The sources can be type library files, registered LIBIDs or ProgIDs, and .NET interop assemblies, also several of them generated together, see Sources.
The other way, cscom typelib export writes the type library of an assembly with [GeneratedComInterface] and
[GeneratedComClass] types, and CsCom.Build exports it after the compilation, see
Exporting type libraries.
Packages
The packages are released together, with the same version.
| Package | |
|---|---|
The cscom .NET tool, which generates projects, assemblies and packages, lists type libraries, manages the CsComReference items of projects and exports type libraries. |
|
| The MSBuild integration, which generates the interop code during the build, and can export the type library of the project. | |
The support code that the generated code uses: IDispatch, the marshallers of VARIANTs, SAFEARRAYs and BSTRs, the late-bound wrappers and the events. |
|
Late binding with C# dynamic. |
What is generated
Vtable and dual interfaces become [GeneratedComInterface] interfaces with extension properties, dispinterfaces become
late-bound wrapper classes, coclasses get Create() methods and event sinks, and enums, structures and modules keep their
COM layout. VARIANTs, BSTRs, SAFEARRAYs, DATE, CURRENCY and VARIANT_BOOL are converted to .NET types, or kept
as wrappers of their own where that matters. See Generated code.