Skip to content

Commit

Permalink
Return false from ComWrappers.Try... methods (#90553)
Browse files Browse the repository at this point in the history
* Return false from ComWrappers.Try... methods

Return false from ComWrappers.TryGetComInstance/TryGetObject instead of
throwing PNSE. It saves callers from needing to protect against PNSE.

Fix #90311

* More efficient IsWindows check
  • Loading branch information
jkotas authored Aug 15, 2023
1 parent 26b1ce7 commit 762030c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ private static TypeDescriptionNode NodeFor(object instance, bool createDelegator
{
type = ComObjectType;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
else if (OperatingSystem.IsWindows()
&& ComWrappers.TryGetComInstance(instance, out nint unknown))
{
// ComObjectType uses the Windows Forms provided ComNativeDescriptor. It currently has hard Win32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public abstract partial class ComWrappers
{
public static unsafe bool TryGetComInstance(object obj, out IntPtr unknown)
{
throw new PlatformNotSupportedException();
unknown = default;
return false;
}

public static unsafe bool TryGetObject(IntPtr unknown, [NotNullWhen(true)] out object? obj)
{
throw new PlatformNotSupportedException();
obj = default;
return false;
}

public partial struct ComInterfaceDispatch
Expand Down

0 comments on commit 762030c

Please sign in to comment.