-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regression in .Net 5 Preview 7 - MarshalDirectiveException is thrown when using the UnmanagedType.HString #39827
Comments
@DmitryGaravsky The built-in support for WinRT API has been removed. This support was removed as a documented breaking change - see #37672. The removal of built-in WinRT support does seem to make the marshalling of Please let us know if the support for |
/cc @jkoritzinsky |
Hi Aaron, thanks for participating Could you please describe in detail why "removing built-in support for WinRT API" affects the PInvoke to Combase.dll in this way? I understand that under the hood the RoGetActivationFactory function is exported by the api-ms-win-core-winrt-l1-1-0.dll. This library is still included in Windows, so it is not quite clear for us why PInvoke fails for now. Are there some ways to tune this behavior? object iface;
int res = RoGetActivationFactory(
HSTRING.FromString("Windows.UI.Notifications.ToastNotificationManager"),
new Guid("50AC103F-D235-4598-BBEF-98FE4D1A3AD4"),
out iface
);
//...
[SecuritySafeCritical, DllImport("api-ms-win-core-winrt-l1-1-0.dll")]
static extern int RoGetActivationFactory(
HSTRING classId,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid guid,
[Out, MarshalAs(UnmanagedType.IUnknown)] out object iface);
[StructLayout(LayoutKind.Sequential), SecuritySafeCritical]
struct HSTRING {
readonly IntPtr handle;
public static HSTRING FromString(string s) {
IntPtr h = Marshal.AllocHGlobal(IntPtr.Size);
Marshal.ThrowExceptionForHR(WindowsCreateString(s, s.Length, h));
return Marshal.PtrToStructure<HSTRING>(h);
}
//
[DllImport("api-ms-win-core-winrt-string-l1-1-0.dll", CallingConvention = CallingConvention.StdCall)]
static extern int WindowsCreateString(
[MarshalAs(UnmanagedType.LPWStr)] string sourceString, int length, [Out] IntPtr hstring);
} Regarding our scenario - we heavily use the demonstrated API to implement a UI component for showing toast notifications from desktop applications( WinForms/WPF). |
The P/Invoke fails because of the usage of Your workaround has a memory leak: It never releases the created You should add a wrapper for
However, even with this workaround, you'll get an |
@DmitryGaravsky These are all good questions.
It impacts this API because the
Because the function call itself isn't the problem, but rather the marshalling of
The marshalling is the issue here, not the call itself. The API might actually fail though because the runtime is no longer marshalling the
That code will work as expected as it relates to the call to |
@DmitryGaravsky I am going to close this for now as the described behavior is expected. Feel free to continue the conversation here or tag myself or @jkoritzinsky if you report issues on the CsWinRT repo. Thanks. |
Description
For now, some COM interop code fails with the MarshalDirectiveException:
Our unit tests detected this issue immediately. (we maintain unit tests for all the released and preview environments like .net core 5 in our codebase).
Thus it is an important breaking change for us.
Configuration
OS Version: Microsoft Windows [Version 10.0.18362.535]
.Net Framework Version: Microsoft (R) .NET CLR Version Tool Version 4.8.3928.0
.Net Core Version: SDK Version: 5.0.100-preview.7.20366.6
Regression?
Yes, this works correctly until .Net Core 5 Preview 7
Other information
Here is the code snippet which fail under .Net Core 5 Preview 7:
RoGetActivationFactory function:
The text was updated successfully, but these errors were encountered: