Skip to content
fedarovichPublic

About

Generates COM interop code from type libraries and .NET interop assemblies, like TlbImp, but with source-generated COM interop (ComWrappers), so it works with trimming and Native AOT. It also exports the type library of an assembly with source-generated COM types, like TlbExp.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

CsCom

Build Documentation CsCom CsCom.Build CsCom.Runtime CsCom.Runtime.Dynamic

CsCom is a command line tool that produces COM interop code from type libraries or existing .NET interop assemblies, similar to TlbImp, and an MSBuild package (CsCom.Build) that generates it during the build of a project. 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.

The documentation is at cscom.fedarovich.com, starting with Getting started.

Requirements

  • Windows: type libraries are read with the OLE Automation APIs. Interop assemblies are read from their metadata.
  • .NET 10 SDK or later. The generated projects target .NET 10 or later.

Usage

# Install the tool
dotnet tool install --global CsCom

# Generate a C# project with the interop types
cscom project create C:\Windows\System32\scrrun.dll -o Interop.Scripting

# Generate several type libraries into one project, each into its own namespace
cscom project create Excel.Application Scripting.Dictionary -n Office.Interop --ns Excel=Office.Excel

# Build a ready-to-use interop assembly, or pack it into a NuGet package
cscom assembly build C:\Windows\System32\scrrun.dll -o bin -p Version=1.0.0
cscom assembly pack C:\Windows\System32\scrrun.dll -o packages --package-version 1.0.0 -p Authors=Contoso

# List the types and members of a type library
cscom list Scripting.Dictionary --type ^IDictionary$

# Generate the interop code during the build of an existing project, with the CsCom.Build package
cscom reference add Excel.Application --namespace Office.Excel

# Export the type library of an assembly with [GeneratedComInterface] and [GeneratedComClass] types, like TlbExp
cscom typelib export bin\Release\net10.0-windows\Server.dll

The generated code:

IDictionary dictionary = Dictionary.Create();       // coclass: CoCreateInstance + ComWrappers
dictionary.CompareMode = CompareMethod.TextCompare; // C# 14 extension property over set_CompareMode
dictionary.Add("a", 1);                             // VARIANT parameters are object?
Console.WriteLine(dictionary.Count);                // C# 14 extension property over get_Count
foreach (object? key in dictionary)                 // _NewEnum via IEnumVARIANT
{
}

Packages

The four packages are released together, with the same version.

Package Description
CsCom The cscom .NET tool: generates projects, assemblies and packages, lists type libraries, manages the CsComReference items of projects and exports type libraries.
CsCom.Build The MSBuild integration, which generates the interop code during the build of a project, and can export the type library of the project.
CsCom.Runtime The support code that the generated code uses: IDispatch, the marshallers, the late-bound wrappers and the events.
CsCom.Runtime.Dynamic Late binding with C# dynamic.

Documentation

The documentation is built from the docs folder. The changes of each version are described in CHANGELOG.md, and the known issues that are not fixed in docs/known-issues.md.

Contributing

dev is the integration branch: create feature branches from it and target pull requests to it. Building, testing and releasing are described in Development, and the projects in Architecture.

dotnet build src/CsCom.slnx
dotnet test src/CsCom.slnx --no-build --filter "TestCategory!=Integration"

License

MIT

About

Generates COM interop code from type libraries and .NET interop assemblies, like TlbImp, but with source-generated COM interop (ComWrappers), so it works with trimming and Native AOT. It also exports the type library of an assembly with source-generated COM types, like TlbExp.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages