Skip to content

Consider using CsWin32 for win32 Interop code #7007

Open

Description

@elachlan

dotnet/winforms is currently working through a migration of the interop code to use CsWin32. The associated issue can be found at dotnet/winforms#7445. CsWin32 makes use of the win32Metadata project. Improvements in the metadata and CsWin32 help improve all projects that make use of it and reduces the amount of interop code maintained in the project.

dotnet/wpf should consider making use of CsWin32 to gain access to these improvements and to help improve the ecosystem.

Activity

  1. added
    Investigate
    and removed
    Investigate
    on Sep 5, 2022
  2. elachlan commented on Sep 7, 2022

    @elachlan
    Author

    @JeremyKuhne I have created a similar issue here in wpf, which if adopted wil be able to make use of the effort done in winforms.

  3. oysteinkrog commented on Sep 12, 2022

    @oysteinkrog

    Is this the same as using LibraryImport source generator?
    It seems like LibraryImport generator is the official story moving forward?

  4. elachlan commented on Sep 12, 2022

    @elachlan
    Author

    @AArnott can explain it better than myself.

    CsWin3w provides the pinvoke stubs and friendly overrides for the win32 API. As well as any structures, enums and constants. It only provides the API surface you request.

    These underlying metadata is provided by win32metadata. Which is used by many different language projections.

    This reduces the code for pinvokes significantly, since it's all generated by CsWin32.

  5. AArnott commented on Sep 13, 2022

    @AArnott

    Is this the same as using LibraryImport source generator?
    It seems like LibraryImport generator is the official story moving forward?

    LibraryImport is essentially just a newer DllImport that works in trimming scenarios where DllImport wouldn't. It emits enough code that the .NET marshaler never has to get involved, which can improve perf. But the heavy lifting of declaring the extern method signature and all the interop types (structs, enums, etc) is still all the developer's responsibility. CsWin32 generates all this for you just by your naming a single API (e.g. CreateFile).

    CsWin32's generated code still uses DllImport (as does LibraryImport, under the covers). So the only remaining issue with using CsWin32 is that it isn't battle hardened for trimmed apps like LibraryImport is. But the LibraryImport folks are talking about exposing their source generator as a library so that other source generators including CsWin32 can consume it. I look forward to consuming this, which will give you all the benefits of LibraryImport's trim-friendly code and the super easy-to-use CsWin32 way of getting whatever API you want just by asking for it. :)

    So that all said, I support the recommendation that you consider using CsWin32 for all your new win32 interop code, and consider migrating existing APIs you hand-wrote over to CsWin32. The future is bright. :)

  6. elachlan commented on Jun 18, 2024

    @elachlan
    Author

    I am bumping here as there has been a massive amount of work done to get Winforms using CsWin32. WPF could make use of that to greatly reduce the conversion time.

  7. McPherran commented on Jul 3, 2025

    @McPherran

    Any existing use of DllImport in select performance-sensitive areas could be updated to LibraryImport.

    • This approach may have less risk even if CsWin32 is updated to leverage LibraryImport (or the underlying generator).
    • For important performance cases, waiting for CsWin32 to be updated may not be a good idea.
    • CsWin32 may not support all cases.
    • CsWin32 may become a tooling dependency.
    • Use of CsWin32 may raise unnecessary regression testing concerns.

    (LibraryImport has been available since .NET 7 and CsWin32's business is interop, so if they still don't use the higher performance P/Invoke, one might consider avoiding it, e.g. if it's not actively developed. Could one unwittingly need to become a CsWin32 developer as well as a WPF developer?)

  8. AArnott commented on Jul 4, 2025

    @AArnott

    CsWin32 may become a dependency.

    Where did this come from? CsWin32 has no runtime component at all.

    It isn't fundamentally slower than LibraryImport. In fact it's not slower at all, particularly if you set allowMarshaling: false. The real takeback is that without marshaling, the API it generates is low level -- perhaps lower level than the API you'd write yourself if using LibraryImport.

    LibraryImport and CsWin32 are not particularly conflicting approaches. If you want to declare all the APIs yourself (as you probably already are), then switching from DllImport to LibraryImport gives you a potential perf boost and works when the runtime mode has marshaling disabled.
    But if you want to get out of the business of declaring the interop APIs altogether, CsWin32 is the way to go.

  9. McPherran commented on Jul 4, 2025

    @McPherran

    It isn't fundamentally slower than LibraryImport. In fact it's not slower at all, particularly if you set allowMarshaling: false.

    Hi,

    If CsWin32 doesn't need to be updated to use LibraryImport and it already matches the performance of LibraryImport then it could be fine. That's an important "if". I certainly appreciate the concept of CsWin32 in general. For an existing framework like WPF one can understand an expedient approach to implementation of performance improvements but also a conservative approach to introducing new tooling.

    I'm not even on the WPF team. I have experience in software at the hardware level as well as corporate and personal experience developing WPF applications. I'm also choosing WPF for a new project, so performance is of interest. That's why I commented here. In fact I was interested in CsWin32 in general until I realized that it may need updating to use LibraryImport. So if somehow that is not important or incorrect, I'm listening, because LibraryImport is clearly the recommended C# P/Invoke attribute, including for performance.

  10. elachlan commented on Jul 4, 2025

    @elachlan
    Author

    CsWin32 is already being used in WPF though? There is a shared library System.Private.Windows.Core being used between WPF and Winforms. It was first developed on Winforms and has been extended for WPF.

    @JeremyKuhne was the one doing that work.

  11. McPherran commented on Jul 4, 2025

    @McPherran

    CsWin32 is already being used in WPF though?

    Hi, A question or a statement?

    The idea is to have performance benefits of LibraryImport (or equivalent). If CsWin32 doesn't provide the performance updates then it is a dependency that takes control of interop performance away from the WPF team. If CsWin32 can deliver the performance updates then it's a plus.

  12. AArnott commented on Jul 4, 2025

    @AArnott

    @McPherran LibraryImport is not an option for source generators, because LibraryImport is not even recognized by the runtime. It's merely a source generator trigger, and ultimately the source generator for LibraryImport creates a DllImport attributed method, as you used to and as CsWin32 still does.

    So CsWin32 will likely never generate LibraryImport attributes, since one source generator cannot feed its output into another source generator (without major hacks).

    LibraryImport is not faster than DllImport, since as I say, the source generated from LibraryImport includes DllImport.
    The difference is that DllImport allows methods to be declared that require the interop marshaler in the runtime. This marshaler is slow (particularly the first time for a given method) and it doesn't work at all in certain runtimes like .NET Native. DllImport itself is not slow -- it's the marshaler that may be used.
    LibraryImport always writes DllImport-attributes methods that don't need the marshaler. Therefore, LibraryImport is "faster" than a poorly written DllImport.

    CsWin32 uses DllImport as well. This isn't fundamentally a bad thing. But some of those DllImport methods still use the marshaler, unless you set the allowMarshaling option in CsWin32 to false, in which case the DllImport methods are always marshal-free and therefore as fast as any LibraryImport attributed method, and as fast as hand-written DllImport methods that also don't require marshaling.

  13. McPherran commented on Jul 4, 2025

    @McPherran

    LibraryImport always writes DllImport-attributes methods that don't need the marshaler. Therefore, LibraryImport is "faster" than a poorly written DllImport.

    https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation

    If I understand correctly much of the performance overhead is based on compile-time vs run-time code processing whereby the LibraryImport provides a compile-time scenario and is better including for AOT. You are also pointing out that this difference is mainly (if not solely) related to marshalling.

    The bottom-line: Does CsWin32 guarantee equal or better performance than what could be achieved for the WPF codebase by converting any uses of DllImport to LibraryImport?

    So far, I haven't heard that guarantee. I have no doubt that you understand the details, and I thank you for the information. However, sometimes details can distract from and dance around the main point.

    • Does CsWin32 itself still need updating to improve its own interop code, e.g. uses of DllImport, to match the performance that the use of LibraryImport provides?
    • Does CsWin32 guarantee equal or better performance than what could be achieved for the WPF codebase by converting any existing uses of DllImport to LibraryImport?
  14. AArnott commented on Jul 4, 2025

    @AArnott

    Does CsWin32 itself still need updating to improve its own interop code, e.g. uses of DllImport, to match the performance that the use of LibraryImport provides?

    Split answer:

    • when using allowMarshaling: true, we rely on the .NET marshaler so some p/invokes will not be as fast as LibraryImport.
    • when using allowMarshaling: false, we do not use the .NET marshaler so it will be as fast as LibraryImport. But the usability of the emitted API in this case may be less friendly than those you might hand-write with LibraryImport.

    So yes, there's room for improvement here because what we'd like to achieve is the usability of allowMarshaling: true combined with the speed you get from allowMarshaling: false. The way we plan to achieve this involves borrowing some code from the LibraryImport source generator to raise the level of marshaling code that CsWin32 is capable of emitting itself.

    Does CsWin32 guarantee equal or better performance than what could be achieved for the WPF codebase by converting any existing uses of DllImport to LibraryImport?

    Again, if allowMarshaling: false then it will be equivalent performance as LibraryImport or a well-written DllImport that intentionally avoids the marshaler.

  15. elachlan commented on Jul 4, 2025

    @elachlan
    Author

    @McPherran you are taking a very narrow view. CSWin32 utilises metadata from the Win32Metadata project. So all the generates methods match the documentation and C++/C headers in windows.

    The abstraction means that the metadata is updated by not only c# consumers, but also other language consumers such as rust.

    Cswin32 is then improved by its consumers.

    The shared library between winforms/wpf can then also be improved by the maintainers and contributors of both projects.

    The structure of the thing means that XML documentation and code quality are much higher than hand rolled interop.

  16. McPherran commented on Jul 4, 2025

    @McPherran
    • when using allowMarshaling: true, ...

    ...allowMarshaling: false, ...

    Thorough answer, thank you. I am considering using CsWin32, so I might inquire further at your repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions