private static AudioDevice CreateDevice(NativeMethods.IMMDevice dev)
            {
                if (dev == null)
                {
                    return(null);
                }

                string id;

                Marshal.ThrowExceptionForHR(dev.GetId(out id));
                Marshal.ThrowExceptionForHR(dev.GetState(out var state));
                Dictionary <string, object> properties = new Dictionary <string, object>();

                Marshal.ThrowExceptionForHR(dev.OpenPropertyStore(NativeMethods.STGM.READ, out var store));
                if (store != null)
                {
                    Marshal.ThrowExceptionForHR(store.GetCount(out var propCount));
                    for (int j = 0; j < propCount; j++)
                    {
                        NativeMethods.PROPERTYKEY pk;
                        if (store.GetAt(j, out pk) == 0)
                        {
                            NativeMethods.PROPVARIANT value = new NativeMethods.PROPVARIANT();
                            int errorCode = store.GetValue(ref pk, ref value);
                            if ((uint)errorCode == NativeMethods.ERROR_NO_SUCH_DEVINST)
                            {
                                continue;
                            }
                            Debug.Assert(errorCode == 0, "Error obtaining information from windows API - error code: " + errorCode);
                            object v = value.GetValue();
                            if (value.vt != NativeMethods.VARTYPE.VT_BLOB) // for some reason, this fails?
                            {
                                errorCode = NativeMethods.PropVariantClear(ref value);
                                Debug.Assert(errorCode == 0, "Error obtaining information from windows API - error code: " + errorCode);
                            }
                            string name = pk.ToString();
                            properties[name] = v;
                        }
                    }
                }
                return(new AudioDevice(id, (AudioDeviceStates)state, properties));
            }
            private static NativeMethods.IAudioSessionManager2 GetAudioSessionManager(NativeMethods.IMMDevice device)
            {
                if (device == null)
                {
                    return(null);
                }

                // win7+ only
                object o;

                if (device.Activate(typeof(NativeMethods.IAudioSessionManager2).GUID, 0, IntPtr.Zero, out o) != 0 || o == null)
                {
                    return(null);
                }

                return(o as NativeMethods.IAudioSessionManager2);
            }