diff --git a/AudioSwitch.csproj b/AudioSwitch.csproj
index 25f63f2..f6114cd 100644
--- a/AudioSwitch.csproj
+++ b/AudioSwitch.csproj
@@ -122,6 +122,7 @@
Code
+
Code
diff --git a/Classes/Program.cs b/Classes/Program.cs
index cb247c1..0a2dd2a 100644
--- a/Classes/Program.cs
+++ b/Classes/Program.cs
@@ -44,7 +44,10 @@ private static void Main(string[] args)
try
{
+#if !DEBUG
+ //log and handle error only if not debugging
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
+#endif
settings = Settings.Load();
if (args.Length > 0)
@@ -225,7 +228,7 @@ private static void Main(string[] args)
}
}
}
-
+#if !DEBUG
static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
@@ -250,6 +253,7 @@ static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEve
Application.Exit();
}
}
+#endif
}
}
diff --git a/Controls/VolumeBar.cs b/Controls/VolumeBar.cs
index 162f110..398c4a4 100644
--- a/Controls/VolumeBar.cs
+++ b/Controls/VolumeBar.cs
@@ -225,7 +225,14 @@ private void VolNotify(AudioVolumeNotificationData data)
internal void RegisterDevice(EDataFlow RenderType)
{
- Device = EndPoints.DeviceEnumerator.GetDefaultAudioEndpoint(RenderType, ERole.eMultimedia);
+ try
+ {
+ Device = EndPoints.DeviceEnumerator.GetDefaultAudioEndpoint(RenderType, ERole.eMultimedia);
+ }
+ catch (DeviceNotFoundException)
+ {//According to previous code state further code won't be called if no devices
+ return;
+ }
Value = Device.AudioEndpointVolume.MasterVolumeLevelScalar;
Mute = Device.AudioEndpointVolume.Mute;
Stereo = Device.AudioMeterInformation.Channels.GetCount() > 1;
diff --git a/CoreAudioApi/DeviceNotFoundException.cs b/CoreAudioApi/DeviceNotFoundException.cs
new file mode 100644
index 0000000..de8be3b
--- /dev/null
+++ b/CoreAudioApi/DeviceNotFoundException.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace AudioSwitch.CoreAudioApi
+{
+ //Inheriting COMException for backward compatibility (the same for HResult in ctor)
+ public class DeviceNotFoundException : COMException
+ {
+ // ReSharper disable once InconsistentNaming
+ internal const int E_NOTFOUND = -2147023728;
+
+ internal DeviceNotFoundException(string message, Exception inner) : base(message, inner)
+ {
+ HResult = E_NOTFOUND;//https://msdn.microsoft.com/en-us/library/windows/desktop/dd371401(v=vs.85).aspx
+ }
+ }
+}
\ No newline at end of file
diff --git a/CoreAudioApi/MMDeviceEnumerator.cs b/CoreAudioApi/MMDeviceEnumerator.cs
index 23a6360..a41f7dd 100644
--- a/CoreAudioApi/MMDeviceEnumerator.cs
+++ b/CoreAudioApi/MMDeviceEnumerator.cs
@@ -43,11 +43,27 @@ public MMDeviceCollection EnumerateAudioEndPoints(EDataFlow dataFlow, EDeviceSta
return new MMDeviceCollection(result);
}
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// If there is no default device
public MMDevice GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role)
{
- IMMDevice _Device;
- Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out _Device));
- return new MMDevice(_Device);
+ try
+ {
+ IMMDevice _Device;
+ Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out _Device));
+ return new MMDevice(_Device);
+
+ }
+ catch (COMException comException) when (comException.ErrorCode == DeviceNotFoundException.E_NOTFOUND)
+ {//if COMException will be used the same way in other methods consider create Exception Mapper instead of manual try/catch
+ throw new DeviceNotFoundException("No devices found during default device detection",comException);
+ }
}
public MMDevice GetDevice(string deviceId)
diff --git a/Forms/FormSwitcher.cs b/Forms/FormSwitcher.cs
index 5da03fe..d5e0ad2 100644
--- a/Forms/FormSwitcher.cs
+++ b/Forms/FormSwitcher.cs
@@ -97,8 +97,7 @@ public FormSwitcher()
SetTrayIcons();
VolBar.VolumeMuteChanged += IconChanged;
- if (listDevices.Items.Count > 0)
- VolBar.RegisterDevice(RenderType);
+ VolBar.RegisterDevice(RenderType);
EndPoints.NotifyClient.DefaultChanged += DefaultChanged;
EndPoints.NotifyClient.DeviceAdded += DeviceAdded;
@@ -402,8 +401,7 @@ private void notifyIcon1_MouseUp(object sender, MouseEventArgs e)
return;
}
- if (listDevices.Items.Count > 0)
- VolBar.RegisterDevice(RenderType);
+ VolBar.RegisterDevice(RenderType);
if (e.Button == MouseButtons.Left)
{
@@ -503,13 +501,16 @@ private void SetDeviceIcon(int index, bool isSelected)
private void SetTrayIcons()
{
- var devSettings = Program.settings.Device.Find(x =>
- {
- var dev = listDevices.SelectedItems[0];
- return x.DeviceID == dev.Tag.ToString();
- });
- if (listDevices.SelectedItems.Count == 0 ||
- devSettings == null ||
+ Settings.CDevice devSettings=null;
+
+ if (listDevices.SelectedItems.Count > 0)
+ devSettings = Program.settings.Device.Find(x =>
+ {
+ var dev = listDevices.SelectedItems[0];
+ return x.DeviceID == dev.Tag.ToString();
+ });
+
+ if (devSettings == null ||
(devSettings.Hue == 0 &&
devSettings.Saturation == 0 &&
devSettings.Brightness == 0))