File size: 1,899 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
namespace Microsoft.PowerShell
{
/// <summary>
/// Represents the OLE struct PROPVARIANT.
/// This class is intended for internal use only.
/// </summary>
/// <remarks>
/// Originally sourced from https://blogs.msdn.com/adamroot/pages/interop-with-propvariants-in-net.aspx
/// and modified to add ability to set values
/// </remarks>
[StructLayout(LayoutKind.Explicit)]
internal sealed class PropVariant : IDisposable
{
// This is actually a VarEnum value, but the VarEnum type requires 4 bytes instead of the expected 2.
[FieldOffset(0)]
private readonly ushort _valueType;
[FieldOffset(8)]
private readonly IntPtr _ptr;
/// <summary>
/// Set a string value.
/// </summary>
internal PropVariant(string value)
{
if (value == null)
{
throw new ArgumentException("PropVariantNullString", nameof(value));
}
_valueType = (ushort)VarEnum.VT_LPWSTR;
_ptr = Marshal.StringToCoTaskMemUni(value);
}
/// <summary>
/// Disposes the object, calls the clear function.
/// </summary>
public void Dispose()
{
PropVariantNativeMethods.PropVariantClear(this);
GC.SuppressFinalize(this);
}
/// <summary>
/// Finalizes an instance of the <see cref="PropVariant"/> class.
/// </summary>
~PropVariant()
{
Dispose();
}
private static class PropVariantNativeMethods
{
[DllImport("Ole32.dll", PreserveSig = false)]
internal static extern void PropVariantClear([In, Out] PropVariant pvar);
}
}
}
|