microsoft / microsoft/CsWin32

Option to only generate 1:1 bindings, and nothing else

Open
#976 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
2.5k
Forks
124
Avg merge
1d 3h
Merged PRs (30d)
9

Description

Overview

Currently, even when setting "disableMarshalling": true in the JSON file, CsWin32 will generate a lot of "helper" stuff by default (eg. friendlier wrappers for P/Invoke-s and generated COM types with managed signatures, vtable types, interface types, etc.). It would be nice to have an option to just have CsWin32 generate the 1:1 bindings for the specified APIs, and literally nothing else. In many cases, these additional wrappers are just not needed at all, because people would just be building their own abstractions on top of them anyway, and keeping them around just increases the IL size unnecessarily. It would be nice to just be able to tell CsWin32

"Literally just get me 1:1 bindings for these APIs as if I was using the C++ header directly"

Describe the solution you'd like

We could have a new "disableManagedHelpers" (or, some other name) boolean flag in the JSON, set to false by default. When set, CsWin32 would stop generating everything that's not directly required by the requested APIs. That is, it would skip:

  • All P/Invoke wrappers
  • All helper methods over COM types
  • All vtable types
  • All interface types for COM structs

Ie. you'd only get raw P/Invoke methods, and raw 1:1 blittable bindings for COM types.

Note: this flag would only cause CsWin32 to skip generating some stuff it's currently generating, so it should be fairly low cost to implement. Just add the new flag and pass it through, and add some checks to skip generating those optional APIs.

Describe alternatives you've considered

Just doing nothing, which would keep the current situation as is.

Additional thoughts

Related to this, but maybe a separate proposal — it would be nice if this also made CsWin32 stop generating all helper wrappers such as HRESULT, HWND, BOOL etc. as well. Instead, the real underlying value should be used, and CsWin32 could just generate a few global using type aliases for them. This would also match C++, as they're just defines there.

As in, something like:

global using BOOL = System.Int32;
global using HWND = System.IntPtr;

internal static extern BOOL SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, winmdroot.UI.WindowsAndMessaging.SET_WINDOW_POS_FLAGS uFlags);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the JSON configuration handles disableMarshalling and how optional APIs are generated. Add and propagate the proposed flag so P/Invoke wrappers, COM helper methods, vtable types, and interface types are skipped while raw bindings remain; done means the requested APIs generate only those raw bindings.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.