From 2522ccd31d28fde0a01cb6a4367dc6f085c42374 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Thu, 20 May 2021 20:52:46 +0600 Subject: [PATCH] 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 - https://github.com/dotnet/runtimelab/pull/1142 Problem is `Marshal.PtrToStringBSTR` throw exception when passed `IntPtr.Zero`, but `Marshal.StringToBSTR` produce `IntPtr.Zero` when given null. --- .../Common/src/System/Runtime/InteropServices/Variant.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libraries/Common/src/System/Runtime/InteropServices/Variant.cs b/src/libraries/Common/src/System/Runtime/InteropServices/Variant.cs index c0eb0c6b819b6..c36ccae8fe81f 100644 --- a/src/libraries/Common/src/System/Runtime/InteropServices/Variant.cs +++ b/src/libraries/Common/src/System/Runtime/InteropServices/Variant.cs @@ -633,6 +633,10 @@ public string AsBstr get { Debug.Assert(VariantType == VarEnum.VT_BSTR); + if (_typeUnion._unionTypes._bstr == IntPtr.Zero) + { + return null; + } return (string)Marshal.PtrToStringBSTR(this._typeUnion._unionTypes._bstr); } set