Skip to content

Commit

Permalink
Added ReleaseDC calls where they were required
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Jan 20, 2017
1 parent 9633c79 commit 698f23f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 2 additions & 0 deletions LightBulb.sln.DotSettings
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DC/@EntryIndexedValue">DC</s:String></wpf:ResourceDictionary>
5 changes: 2 additions & 3 deletions LightBulb/Services/Abstract/WinApiServiceBase.cs
Expand Up @@ -13,11 +13,10 @@ protected Win32Exception GetLastError()
return new Win32Exception(errCode);
}

protected void CheckThrowWin32Error()
protected void CheckLogWin32Error()
{
var ex = GetLastError();
//if (ex != null) throw ex;
//if (ex != null) Debug.WriteLine($"Win32 error: {ex.Message} ({ex.NativeErrorCode})", GetType().Name);
if (ex != null) Debug.WriteLine($"Win32 error: {ex.Message} ({ex.NativeErrorCode})", GetType().Name);
}
}
}
15 changes: 10 additions & 5 deletions LightBulb/Services/GammaService.cs
Expand Up @@ -9,13 +9,16 @@ namespace LightBulb.Services
public class GammaService : WinApiServiceBase
{
#region WinAPI
[DllImport("user32.dll", EntryPoint = "GetDC", SetLastError = false)]
[DllImport("user32.dll", EntryPoint = "GetDC", SetLastError = true)]
private static extern IntPtr GetDCInternal(IntPtr hWnd);

[DllImport("gdi32.dll", EntryPoint = "SetDeviceGammaRamp", SetLastError = false)]
[DllImport("user32.dll", EntryPoint = "ReleaseDC", SetLastError = true)]
private static extern int ReleaseDCInternal(IntPtr hWnd, IntPtr hDC);

[DllImport("gdi32.dll", EntryPoint = "SetDeviceGammaRamp", SetLastError = true)]
private static extern bool SetDeviceGammaRampInternal(IntPtr hMonitor, ref GammaRamp ramp);

[DllImport("gdi32.dll", EntryPoint = "GetDeviceGammaRamp", SetLastError = false)]
[DllImport("gdi32.dll", EntryPoint = "GetDeviceGammaRamp", SetLastError = true)]
private static extern bool GetDeviceGammaRampInternal(IntPtr hMonitor, out GammaRamp ramp);
#endregion

Expand All @@ -36,7 +39,8 @@ private GammaRamp GetDisplayGammaRamp()
var dc = GetDCInternal(IntPtr.Zero);
GammaRamp ramp;
if (!GetDeviceGammaRampInternal(dc, out ramp))
CheckThrowWin32Error();
CheckLogWin32Error();
ReleaseDCInternal(IntPtr.Zero, dc);
return ramp;
}

Expand All @@ -55,7 +59,8 @@ private void SetDisplayGammaRamp(GammaRamp ramp)
// Set ramp
var dc = GetDCInternal(IntPtr.Zero);
if (!SetDeviceGammaRampInternal(dc, ref ramp))
CheckThrowWin32Error();
CheckLogWin32Error();
ReleaseDCInternal(IntPtr.Zero, dc);
}

/// <summary>
Expand Down
20 changes: 10 additions & 10 deletions LightBulb/Services/WindowService.cs
Expand Up @@ -14,24 +14,24 @@ public class WindowService : WinApiServiceBase, IDisposable
IntPtr hwnd, int idObject, int idChild, uint dwEventThread,
uint dwmsEventTime);

[DllImport("user32.dll", EntryPoint = "GetForegroundWindow", SetLastError = false)]
[DllImport("user32.dll", EntryPoint = "GetForegroundWindow", SetLastError = true)]
private static extern IntPtr GetForegroundWindowInternal();

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow", SetLastError = false)]
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow", SetLastError = true)]
private static extern IntPtr GetDesktopWindowInternal();

[DllImport("user32.dll", EntryPoint = "GetShellWindow", SetLastError = false)]
[DllImport("user32.dll", EntryPoint = "GetShellWindow", SetLastError = true)]
private static extern IntPtr GetShellWindowInternal();

[DllImport("user32.dll", EntryPoint = "GetWindowRect", SetLastError = false)]
[DllImport("user32.dll", EntryPoint = "GetWindowRect", SetLastError = true)]
private static extern int GetWindowRectInternal(IntPtr hWindow, out Rect rect);

[DllImport("user32.dll", EntryPoint = "SetWinEventHook", SetLastError = false)]
[DllImport("user32.dll", EntryPoint = "SetWinEventHook", SetLastError = true)]
private static extern IntPtr SetWinEventHookInternal(uint eventMin, uint eventMax,
IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc,
uint idProcess, uint idThread, uint dwFlags);

[DllImport("user32.dll", EntryPoint = "UnhookWinEvent", SetLastError = false)]
[DllImport("user32.dll", EntryPoint = "UnhookWinEvent", SetLastError = true)]
private static extern bool UnhookWinEventInternal(IntPtr hWinEventHook);
#endregion

Expand Down Expand Up @@ -142,7 +142,7 @@ private void UninstallHooks()
public IntPtr GetForegroundWindow()
{
var result = GetForegroundWindowInternal();
CheckThrowWin32Error();
CheckLogWin32Error();
return result;
}

Expand All @@ -152,7 +152,7 @@ public IntPtr GetForegroundWindow()
public IntPtr GetDesktopWindow()
{
var result = GetDesktopWindowInternal();
CheckThrowWin32Error();
CheckLogWin32Error();
return result;
}

Expand All @@ -162,7 +162,7 @@ public IntPtr GetDesktopWindow()
public IntPtr GetShellWindow()
{
var result = GetShellWindowInternal();
CheckThrowWin32Error();
CheckLogWin32Error();
return result;
}

Expand All @@ -173,7 +173,7 @@ public Rect GetWindowRect(IntPtr hWindow)
{
Rect result;
GetWindowRectInternal(hWindow, out result);
CheckThrowWin32Error();
CheckLogWin32Error();
return result;
}

Expand Down

0 comments on commit 698f23f

Please sign in to comment.