public void SetDefaultAudioDevice(SoundControllServer.AudioDevice device, AudioDeviceRole role)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            // BADNESS: The following code uses undocumented interfaces provided by the Audio SDK. This is completely
            // unsupported, and should be used for amusement purposes only. This is *extremely likely* to be broken
            // in future updates and/or versions of Windows. If Larry Osterman was dead, he would be rolling over
            // in his grave if he knew you were using this for nefarious purposes.
            var config = new PolicyConfig();

            int            hr;
            IPolicyConfig2 config2 = config as IPolicyConfig2;

            if (config2 != null)
            {   // Windows 7 -> Windows 8.1
                hr = config2.SetDefaultEndpoint(device.Id, role);
            }
            else
            {   // Windows 10+
                hr = ((IPolicyConfig3)config).SetDefaultEndpoint(device.Id, role);
            }

            if (hr != HResult.OK)
            {
                throw Marshal.GetExceptionForHR(hr);
            }
        }
Exemplo n.º 2
0
 public static void SetDefaultAudioDevice(MMDevice mMDevice)
 {
     try
     {
         var            config = new PolicyConfig();
         int            hr1;
         int            hr2;
         int            hr3;
         bool?          win10   = null;
         IPolicyConfig2 config2 = config as IPolicyConfig2;
         if (config2 != null)
         {
             win10 = false;
             hr1   = config2.SetDefaultEndpoint(mMDevice.ID, AudioDeviceRole.Multimedia);
             hr2   = config2.SetDefaultEndpoint(mMDevice.ID, AudioDeviceRole.Communications);
             hr3   = config2.SetDefaultEndpoint(mMDevice.ID, AudioDeviceRole.Console);
         }
         else
         {
             win10 = true;
             hr1   = ((IPolicyConfig3)config).SetDefaultEndpoint(mMDevice.ID, AudioDeviceRole.Multimedia);
             hr2   = ((IPolicyConfig3)config).SetDefaultEndpoint(mMDevice.ID, AudioDeviceRole.Communications);
             hr3   = ((IPolicyConfig3)config).SetDefaultEndpoint(mMDevice.ID, AudioDeviceRole.Console);
         }
         Toolbox.uAddDebugLog($"NAudio HResult Definitions: s_OK({HResult.S_OK}) | s_FALSE({HResult.S_FALSE}) | e_INVALIDARG({HResult.E_INVALIDARG})");
         Toolbox.uAddDebugLog($"SetAudioDefaultResults: win10({win10}) | hr1({hr1}) | hr2({hr2}) | hr3({hr3})");
     }
     catch (Exception ex)
     {
         Toolbox.LogException(ex);
     }
 }