Bug: CsWinRT 3 undetermined-interface case relies on undefined COM behavior
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 665
- Forks
- 134
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 32
Description
Description
When CsWinRT could not determine which interface to use in the marshaller, it falls back to using IInspectable (see code below). However, performing a QueryInterface to IInspectable is undefined behavior.
https://github.com/microsoft/CsWinRT/blob/228e89e3ffcb4dbaba6af7e0365e3a52966bcd83/src/WinRT.Runtime2/InteropServices/WindowsRuntimeComWrappers.cs#L439-L445
Steps To Reproduce
[This sample assumes that you have OneDrive installed with an account signed in and the machine runs Windows 11 version 24H2 or higher]
- Go to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SyncRootManager - Find subkeys with the prefix
OneDrive!, and copy the subkey name for any one of them. - Create a console project, using CsWinRT 3 and enabling unsafe code
- Paste the following code in, replacing the
srivariable with the subkey name copied above.
using Microsoft.Win32;
using System.Runtime.InteropServices;
using Windows.Storage.Provider;
using WindowsRuntime.InteropServices;
string sri = "[subkey name]";
using var key = Registry.LocalMachine.OpenSubKey($"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SyncRootManager\\{sri}")!;
Guid factoryClsid = Guid.Parse((string)key.GetValue("StorageProviderStatusUISourceFactory")!);
IStorageProviderStatusUISourceFactory factory;
unsafe
{
Marshal.ThrowExceptionForHR(PInvoke.CoCreateInstance(factoryClsid, null, 0x4, typeof(IStorageProviderStatusUISourceFactory).GUID, out void* ppv));
factory = (IStorageProviderStatusUISourceFactory)WindowsRuntimeMarshal.ConvertToManaged(ppv)!;
var statusUISource = factory.GetStatusUISource(sri);
Console.WriteLine(statusUISource.GetStatusUI().QuotaUI.QuotaUsedLabel);
}
static partial class PInvoke
{
[LibraryImport("combase")]
public static unsafe partial int CoCreateInstance(
in Guid rclsid,
void* pUnkOuter,
uint dwClsContext,
in Guid riid,
out void* ppv);
}
- Run the project, observe
InvalidCastExceptionat thestatusUISourceline.
Expected Behavior
The console should print the storage usage.
Version Info
CsWinRT 3.0.0-preview.260319.2 (latest on NuGet)
Additional Context
This is essentially the same issue as https://github.com/microsoft/CsWin32/issues/1716, but for WinRT
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/WinRT.Runtime2/InteropServices/WindowsRuntimeComWrappers.cs around lines 439-445, then reproduce the provided OneDrive sample on Windows 11 24H2 or later. Trace the undetermined-interface marshalling path and verify the fix by rerunning the sample; done means it no longer throws InvalidCastException and prints the storage usage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop, operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100