public static IList <AudioDevice> RequestAllDevices()
            {
                List <AudioDevice> list = new List <AudioDevice>();

                NativeMethods.IMMDeviceEnumerator deviceEnumerator = null;
                deviceEnumerator = (NativeMethods.IMMDeviceEnumerator)(new NativeMethods.MMDeviceEnumerator());
                if (deviceEnumerator == null)
                {
                    return(list);
                }

                NativeMethods.IMMDeviceCollection collection;
                Marshal.ThrowExceptionForHR(deviceEnumerator.EnumAudioEndpoints(NativeMethods.EDataFlow.eRender, NativeMethods.DEVICE_STATE.MASK_ALL, out collection));
                if (collection == null)
                {
                    return(list);
                }

                int count;

                Marshal.ThrowExceptionForHR(collection.GetCount(out count));
                for (int i = 0; i < count; i++)
                {
                    NativeMethods.IMMDevice dev;
                    Marshal.ThrowExceptionForHR(collection.Item(i, out dev));
                    if (dev != null)
                    {
                        list.Add(CreateDevice(dev));
                    }
                }
                return(list);
            }
            private static NativeMethods.IMMDevice GetDefaultSpeakers()
            {
                // get the speakers (1st render + multimedia) device
                NativeMethods.IMMDeviceEnumerator deviceEnumerator = (NativeMethods.IMMDeviceEnumerator)(new NativeMethods.MMDeviceEnumerator());
                NativeMethods.IMMDevice           speakers;
                int errorCode = deviceEnumerator.GetDefaultAudioEndpoint(NativeMethods.EDataFlow.eRender, NativeMethods.ERole.eMultimedia, out speakers);

                Debug.Assert(errorCode == 0, "Error obtaining information from windows API - error code: " + errorCode);
                return(speakers);
            }
            public static IList <AudioSession> RequestAllSessions(AudioDevice device)
            {
                if (device == null)
                {
                    throw new ArgumentNullException(nameof(device));
                }

                List <AudioSession> list = new List <AudioSession>();

                NativeMethods.IMMDeviceEnumerator deviceEnumerator = (NativeMethods.IMMDeviceEnumerator)(new NativeMethods.MMDeviceEnumerator());
                NativeMethods.IMMDevice           immDevice;
                Marshal.ThrowExceptionForHR(deviceEnumerator.GetDevice(device.Id, out immDevice));
                Debug.Assert(immDevice != null, "Unable to retrieve IMMDevice with EnumeratorName of AudioDevice");
                NativeMethods.IAudioSessionManager2 mgr = GetAudioSessionManager(immDevice);
                if (mgr == null)
                {
                    return(list);
                }

                NativeMethods.IAudioSessionEnumerator sessionEnumerator;
                Marshal.ThrowExceptionForHR(mgr.GetSessionEnumerator(out sessionEnumerator));
                int count;

                Marshal.ThrowExceptionForHR(sessionEnumerator.GetCount(out count));

                for (int i = 0; i < count; i++)
                {
                    NativeMethods.IAudioSessionControl ctl;
                    Marshal.ThrowExceptionForHR(sessionEnumerator.GetSession(i, out ctl));
                    if (ctl == null)
                    {
                        continue;
                    }

                    NativeMethods.IAudioSessionControl2 ctl2 = ctl as NativeMethods.IAudioSessionControl2;
                    NativeMethods.ISimpleAudioVolume    sav  = ctl as NativeMethods.ISimpleAudioVolume;
                    if (ctl2 != null && sav != null)
                    {
                        list.Add(new AudioSession(ctl2, sav));
                    }
                }
                Marshal.ReleaseComObject(sessionEnumerator);
                Marshal.ReleaseComObject(mgr);
                return(list);
            }