diff --git a/src/System.Management.Automation/engine/COM/ComInvoker.cs b/src/System.Management.Automation/engine/COM/ComInvoker.cs
index 3834be3de8..a1b4a4c1ba 100644
--- a/src/System.Management.Automation/engine/COM/ComInvoker.cs
+++ b/src/System.Management.Automation/engine/COM/ComInvoker.cs
@@ -280,7 +280,7 @@ namespace System.Management.Automation
{
for (int i = 0; i < argCount; i++)
{
- VariantClear(variantArgArray + s_variantSize * i);
+ Interop.Windows.VariantClear(variantArgArray + s_variantSize * i);
}
Marshal.FreeCoTaskMem(variantArgArray);
@@ -297,7 +297,7 @@ namespace System.Management.Automation
{
for (int i = 0; i < refCount; i++)
{
- VariantClear(tmpVariants + s_variantSize * i);
+ Interop.Windows.VariantClear(tmpVariants + s_variantSize * i);
}
Marshal.FreeCoTaskMem(tmpVariants);
@@ -305,13 +305,6 @@ namespace System.Management.Automation
}
}
- ///
- /// Clear variables of type VARIANTARG (or VARIANT) before the memory containing the VARIANTARG is freed.
- ///
- ///
- [DllImport("oleaut32.dll")]
- internal static extern void VariantClear(IntPtr pVariant);
-
///
/// We have to declare 'bstrSource', 'bstrDescription' and 'bstrHelpFile' as pointers because
/// CLR marshalling layer would try to free those BSTRs by default and that is not correct.
diff --git a/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs b/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs
index 84a31b2642..941f471b66 100644
--- a/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs
+++ b/src/System.Management.Automation/engine/ComInterop/InteropServices/Variant.cs
@@ -276,9 +276,6 @@ namespace System.Management.Automation.InteropServices
}
}
- [DllImport("oleaut32.dll")]
- internal static extern void VariantClear(IntPtr variant);
-
///
/// Release any unmanaged memory associated with the Variant
///
@@ -306,7 +303,7 @@ namespace System.Management.Automation.InteropServices
{
fixed (void* pThis = &this)
{
- VariantClear((IntPtr)pThis);
+ Interop.Windows.VariantClear((nint)pThis);
}
}
diff --git a/src/System.Management.Automation/engine/Interop/Windows/VariantClear.cs b/src/System.Management.Automation/engine/Interop/Windows/VariantClear.cs
new file mode 100644
index 0000000000..a3c0ce70d8
--- /dev/null
+++ b/src/System.Management.Automation/engine/Interop/Windows/VariantClear.cs
@@ -0,0 +1,17 @@
+// Copyright (c) Microsoft Corporation.
+// Licensed under the MIT License.
+
+#nullable enable
+
+#if !UNIX
+using System.Runtime.InteropServices;
+
+internal static partial class Interop
+{
+ internal static partial class Windows
+ {
+ [LibraryImport("oleaut32.dll")]
+ internal static partial void VariantClear(nint pVariant);
+ }
+}
+#endif