Skip to content

Commit 999c9c0

Browse files
authored
Return null when Variant contains BSTR (#53030)
* Return null when Variant contains BSTR I found this one when implement VARIANT marshalling for NativeAOT. Without that I have to resort to duplicate implementation in that class. PR where I discover this - dotnet/runtimelab#1142 Problem is `Marshal.PtrToStringBSTR` throw exception when passed `IntPtr.Zero`, but `Marshal.StringToBSTR` produce `IntPtr.Zero` when given null. * Update nullable annotations
1 parent 8b4684e commit 999c9c0

File tree

1 file changed

+5
-1
lines changed
  • src/libraries/Common/src/System/Runtime/InteropServices

1 file changed

+5
-1
lines changed

src/libraries/Common/src/System/Runtime/InteropServices/Variant.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,15 @@ public DateTime AsDate
628628

629629
// VT_BSTR
630630

631-
public string AsBstr
631+
public string? AsBstr
632632
{
633633
get
634634
{
635635
Debug.Assert(VariantType == VarEnum.VT_BSTR);
636+
if (_typeUnion._unionTypes._bstr == IntPtr.Zero)
637+
{
638+
return null;
639+
}
636640
return (string)Marshal.PtrToStringBSTR(this._typeUnion._unionTypes._bstr);
637641
}
638642
set

0 commit comments

Comments
 (0)