public static void SetApplicationMute(int pid, bool mute)
        {
            Interfaces.ISimpleAudioVolume volume = GetVolumeObject(pid);
            if (volume == null)
            {
                throw new Exception("No application found for pid");
            }

            Guid guid = Guid.Empty;

            volume.SetMute(mute, guid);
            Marshal.ReleaseComObject(volume);
        }
        public static void SetSystemSoundsMute(bool mute)
        {
            Interfaces.ISimpleAudioVolume volume = GetSystemSoundsVolumeObject();
            if (volume == null)
            {
                throw new Exception("No session found for system sounds");
            }

            Guid guid = Guid.Empty;

            volume.SetMute(mute, guid);
            Marshal.ReleaseComObject(volume);
        }
        public static void SetApplicationVolume(int pid, float level)
        {
            Interfaces.ISimpleAudioVolume volume = GetVolumeObject(pid);
            if (volume == null)
            {
                throw new Exception("No application found for pid");
            }

            Guid guid = Guid.Empty;

            volume.SetMasterVolume(level / 100, guid);
            Marshal.ReleaseComObject(volume);
        }
        public static bool?GetSystemSoundsMute()
        {
            Interfaces.ISimpleAudioVolume volume = GetSystemSoundsVolumeObject();
            if (volume == null)
            {
                throw new Exception("No session found for system sounds");
            }

            bool mute;

            volume.GetMute(out mute);
            Marshal.ReleaseComObject(volume);
            return(mute);
        }
        public static float?GetSystemSoundsVolume()
        {
            Interfaces.ISimpleAudioVolume volume = GetSystemSoundsVolumeObject();
            if (volume == null)
            {
                throw new Exception("No session found for system sounds");
            }

            float level;

            volume.GetMasterVolume(out level);
            Marshal.ReleaseComObject(volume);
            return(level * 100);
        }
        public static bool?GetApplicationMute(int pid)
        {
            Interfaces.ISimpleAudioVolume volume = GetVolumeObject(pid);
            if (volume == null)
            {
                throw new Exception("No application found for pid");
            }

            bool mute;

            volume.GetMute(out mute);
            Marshal.ReleaseComObject(volume);
            return(mute);
        }
        public static float?GetApplicationVolume(int pid)
        {
            Interfaces.ISimpleAudioVolume volume = GetVolumeObject(pid);
            if (volume == null)
            {
                throw new Exception("No application found for pid");
            }

            float level;

            volume.GetMasterVolume(out level);
            Marshal.ReleaseComObject(volume);
            return(level * 100);
        }
        private static Interfaces.ISimpleAudioVolume GetSystemSoundsVolumeObject()
        {
            Interfaces.IMMDeviceEnumerator     deviceEnumerator  = null;
            Interfaces.IAudioSessionEnumerator sessionEnumerator = null;
            Interfaces.IAudioSessionManager2   mgr = null;
            Interfaces.IMMDevice speakers          = null;
            try
            {
                // get the speakers (1st render + multimedia) device
                deviceEnumerator = (Interfaces.IMMDeviceEnumerator)(new Interfaces.MMDeviceEnumerator());
                deviceEnumerator.GetDefaultAudioEndpoint(Interfaces.EDataFlow.eRender, Interfaces.ERole.eMultimedia, out speakers);

                // activate the session manager. we need the enumerator
                Guid   IID_IAudioSessionManager2 = typeof(Interfaces.IAudioSessionManager2).GUID;
                object o;
                speakers.Activate(IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
                mgr = (Interfaces.IAudioSessionManager2)o;

                // enumerate sessions for on this device
                mgr.GetSessionEnumerator(out sessionEnumerator);
                int count;
                sessionEnumerator.GetCount(out count);

                // search for an audio session with the required process-id
                Interfaces.ISimpleAudioVolume volumeControl = null;
                for (int i = 0; i < count; ++i)
                {
                    Interfaces.IAudioSessionControl2 ctl = null;
                    try
                    {
                        sessionEnumerator.GetSession(i, out ctl);
                        ctl.GetDisplayName(out string val);
                        if (val.ToLower().Contains("@%systemroot%\\system32\\audiosrv.dll"))
                        {
                            volumeControl = ctl as Interfaces.ISimpleAudioVolume;
                            break;
                        }
                    }
                    finally
                    {
                    }
                }

                return(volumeControl);
            }
            finally
            {
                if (sessionEnumerator != null)
                {
                    Marshal.ReleaseComObject(sessionEnumerator);
                }
                if (mgr != null)
                {
                    Marshal.ReleaseComObject(mgr);
                }
                if (speakers != null)
                {
                    Marshal.ReleaseComObject(speakers);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
        }