From 7678ee102cc643b464c5021aa0f26bcb7e1b5a3e Mon Sep 17 00:00:00 2001 From: David Hall Date: Sat, 27 Jan 2018 14:34:39 -0700 Subject: [PATCH] Fixed problem with DwmGetWindowAttribute and DwmSetWindowAttribute helper functions not handling enum values. --- PInvoke/DwmApi/DwmApi.cs | 10 ++++++---- UnitTests/PInvoke/DwmApi/DwmApiTests.cs | 5 ++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/PInvoke/DwmApi/DwmApi.cs b/PInvoke/DwmApi/DwmApi.cs index c260ca96..04405600 100644 --- a/PInvoke/DwmApi/DwmApi.cs +++ b/PInvoke/DwmApi/DwmApi.cs @@ -459,10 +459,12 @@ namespace Vanara.PInvoke public static HRESULT DwmGetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, out T pvAttribute) { if (!CorrespondingTypeAttribute.CanGet(dwAttribute, typeof(T))) throw new ArgumentException(); - var isBool = typeof(T) == typeof(bool); - var m = new SafeCoTaskMemHandle(Marshal.SizeOf(isBool ? typeof(uint) : typeof(T))); + var type = typeof(T); + var isBool = type == typeof(bool); + type = isBool ? typeof(uint) : (type.IsEnum ? Enum.GetUnderlyingType(type) : type); + var m = new SafeCoTaskMemHandle(Marshal.SizeOf(type)); var hr = DwmGetWindowAttribute(hwnd, dwAttribute, (IntPtr)m, m.Size); - pvAttribute = isBool ? (T)Convert.ChangeType(m.ToStructure(), typeof(bool)) : m.ToStructure(); + pvAttribute = isBool ? (T)Convert.ChangeType(m.ToStructure(), typeof(bool)) : (T)Marshal.PtrToStructure((IntPtr)m, type); return hr; } @@ -618,7 +620,7 @@ namespace Vanara.PInvoke public static HRESULT DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, [In] T pvAttribute) { if (pvAttribute == null || !CorrespondingTypeAttribute.CanSet(dwAttribute, typeof(T))) throw new ArgumentException(); - var attr = pvAttribute is bool ? (object)Convert.ToUInt32(pvAttribute) : pvAttribute; + var attr = pvAttribute is bool ? Convert.ToUInt32(pvAttribute) : (pvAttribute.GetType().IsEnum ? Convert.ChangeType(pvAttribute, Enum.GetUnderlyingType(pvAttribute.GetType())) : pvAttribute); using (var p = new PinnedObject(attr)) return DwmSetWindowAttribute(hwnd, dwAttribute, p, Marshal.SizeOf(attr)); } diff --git a/UnitTests/PInvoke/DwmApi/DwmApiTests.cs b/UnitTests/PInvoke/DwmApi/DwmApiTests.cs index d6653203..f1b5c15e 100644 --- a/UnitTests/PInvoke/DwmApi/DwmApiTests.cs +++ b/UnitTests/PInvoke/DwmApi/DwmApiTests.cs @@ -74,7 +74,7 @@ namespace Vanara.PInvoke.Tests err = DwmGetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_NCRENDERING_ENABLED, out bool b2); if (err.Failed) TestContext.WriteLine($"Err:DWMWA_NCRENDERING_ENABLE={err}"); Assert.That(err.Succeeded); - err = DwmGetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_CLOAKED, out int i2); + err = DwmGetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_CLOAKED, out DWM_CLOAKED i2); if (err.Failed) TestContext.WriteLine($"Err:DWMWA_NCRENDERING_POLICY={err}"); Assert.That(err.Succeeded); wnd.Hide(); @@ -98,6 +98,9 @@ namespace Vanara.PInvoke.Tests var err = DwmSetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_CLOAK, true); if (err.Failed) TestContext.WriteLine($"Err:DWMWA_CLOAK={err}"); Assert.That(err.Succeeded); + err = DwmSetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_NCRENDERING_POLICY, DWMNCRENDERINGPOLICY.DWMNCRP_USEWINDOWSTYLE); + if (err.Failed) TestContext.WriteLine($"Err:DWMWA_NCRENDERING_POLICY={err}"); + Assert.That(err.Succeeded); wnd.Hide(); } }