static object ToObject(this MMDeviceAPI.tag_inner_PROPVARIANT propvariant)
        {
            var marshalledPropvariant = propvariant.Marshall();

            PropVariantToVariant(ref marshalledPropvariant, out var variant);
            PropVariantClear(ref marshalledPropvariant);
            return(variant);
        }
        static Propvariant Marshall(this MMDeviceAPI.tag_inner_PROPVARIANT propvariant)
        {
            // Note: it seems unlikely this will work with complicated/exotic PROPVARIANT types.
            var marshalledPropvariant = new Propvariant();

            marshalledPropvariant.vt = propvariant.vt;
            // TODO: it's not clear if this autogenerated field name is stable.
            marshalledPropvariant.p = propvariant.__MIDL____MIDL_itf_mmdeviceapi_0003_00930001.pcVal;
            return(marshalledPropvariant);
        }
        public static T ActivateInterface <T>(this MMDeviceAPI.IMMDevice device) where T : class
        {
            var guid = new System.Guid(typeof(T).GetCustomAttribute <System.Runtime.InteropServices.GuidAttribute>(false).Value);
            // Normally we're supposed to pass NULL as pActivationParams but C# won't let us pass null to a ref struct argument. Passing an empty PROPVARIANT seems to work fine.
            var activationParams = new MMDeviceAPI.tag_inner_PROPVARIANT();

            device.Activate(ref guid, (uint)CLSCTX.CLSCTX_ALL, ref activationParams, out var interfacePtr);
            object iface;

            try
            {
                iface = System.Runtime.InteropServices.Marshal.GetTypedObjectForIUnknown(interfacePtr, typeof(T));
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.Release(interfacePtr);
            }
            return(iface as T);
        }