microsoft / microsoft/CsWinRT

Bug: cswinrt uses wrong architecture runtime dll and will cause a runtime exception

Open
#2,094 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
665
Forks
134
Avg merge
1d 3h
Merged PRs (30d)
32

Description

Description

This issue is related to the following ones. I recommend you must have a check on these. Please review them, as I don’t want to repeat the details here:
#1522 #1097 #1874
I believe the cswinrt nuget package used in the project has an architecture mismatch issue. Whether I manually assigned x64 or anyCPU(I'm not entirely sure what's the anyCPU's actually meaning, and it also doesn't work either), running the solution will cause '%1 is not a valid Win32 application.' hresult error. Only when all build configurations are set to x86 does the solution run successfully.
Please review all the related issues and make sure you’ve readgithub link for more details!
Some related CsWinRT DLLs appear to have been built for the wrong architecture. I have checked some dlls using Visual Studio dumpbin tools and the results are as follows. There are also some others dlls you can check by yourself. I'm not sure if this is the root cause of the x64 issue, but if CsWinRT uses runtime DLLs built for the wrong architecture, it could trigger a runtime exception.

Image Image Image Image

And I don't know why the project is automatically linked with .net6.0 but my project is .net10 and it has a .net8.0 in the same folder. maybe you should update the .net dependence version for nuget? I'm not sure it's good or not.

Image And I always occure this error, maybe it's vs bug Image
Steps To Reproduce

All CsWinRT projects must assigne as x86 architecture will run without error. So the arm and x64 solution can't use

Image I create two projects. And my goal is to use C# compoent by cswinrt for WinUI3 cppwinRT, so I create a WinUI class library Image And I made a small example that you can easily verify as what I said before, please check https://github.com/hoshiizumiya/WinUI3-CppWinRT-CsRuntimeComponent-Sample.git
Expected Behavior

Each architecture build option should run property, specific anyCPU effect.

Version Info

CsWinRT 2.2.0
VS 2026 11121.172

Additional Context

And I mostly referred these guides

https://learn.microsoft.com/en-us/windows/apps/develop/platform/csharp-winrt/create-winrt-component-winui-cswinrt#reference-the-component-from-a-windows-app-sdk-cwinrt-app
https://github.com/microsoft/CsWinRT/blob/master/docs/authoring.md#consuming-from-c-applications-1

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 reviewing related issues #1522, #1097, and #1874, then reproduce the failure with the linked WinUI3-CppWinRT-CsRuntimeComponent-Sample project. Use the reported dumpbin checks to compare runtime DLL architectures across x86, x64, ARM, and AnyCPU builds. Done means each supported architecture runs without the reported Win32 application exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.