Skip to content
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

Minor improvements to 'ExceptionHelpers.SetErrorInfo' #1824

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public static object TryRegisterObjectForInterface(object obj, IntPtr thisPtr)
return ComWrappers.GetOrRegisterObjectForComInstance(thisPtr, CreateObjectFlags.TrackerObject, obj);
}

internal static IntPtr CreateCCWForObjectUnsafe(object obj)
{
return ComWrappers.GetOrCreateComInterfaceForObject(obj, CreateComInterfaceFlags.TrackerSupport);
}

public static IObjectReference CreateCCWForObject(object obj)
{
IntPtr ccw = ComWrappers.GetOrCreateComInterfaceForObject(obj, CreateComInterfaceFlags.TrackerSupport);
Expand Down
28 changes: 23 additions & 5 deletions src/WinRT.Runtime/ExceptionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,36 @@ public static unsafe void SetErrorInfo(Exception ex)
message = ex.GetType().FullName;
}

IntPtr hstring;
IntPtr hstringHeader = IntPtr.Zero;
IntPtr hstring = IntPtr.Zero;

fixed (char* lpMessage = message)
{
if (Platform.WindowsCreateString((ushort*)lpMessage, message.Length, &hstring) != 0)
if (Platform.WindowsCreateStringReference(
sourceString: (ushort*)lpMessage,
length: message.Length,
hstring_header: &hstringHeader,
manodasanW marked this conversation as resolved.
Show resolved Hide resolved
hstring: &hstring) != 0)
{
hstring = IntPtr.Zero;
}
}

using var managedExceptionWrapper = ComWrappersSupport.CreateCCWForObject(ex);
roOriginateLanguageException(GetHRForException(ex), hstring, managedExceptionWrapper.ThisPtr);
#if NET
IntPtr managedExceptionWrapper = ComWrappersSupport.CreateCCWForObjectUnsafe(ex);

try
{
roOriginateLanguageException(GetHRForException(ex), hstring, managedExceptionWrapper);
}
finally
{
Marshal.Release(managedExceptionWrapper);
}
#else
using var managedExceptionWrapper = ComWrappersSupport.CreateCCWForObject(ex);
roOriginateLanguageException(GetHRForException(ex), hstring, managedExceptionWrapper.ThisPtr);
#endif
}
}
}
else
Expand Down