From cd2b5e62ff3b103874a0c5ec20248d85fc17392b Mon Sep 17 00:00:00 2001 From: David Hall Date: Sat, 27 Jan 2018 10:45:40 -0700 Subject: [PATCH] Updated Get/SetAttr methods to use CorrespondingTypeAttribute validation. --- PInvoke/DwmApi/DwmApi.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PInvoke/DwmApi/DwmApi.cs b/PInvoke/DwmApi/DwmApi.cs index e25c5944..c260ca96 100644 --- a/PInvoke/DwmApi/DwmApi.cs +++ b/PInvoke/DwmApi/DwmApi.cs @@ -458,8 +458,8 @@ namespace Vanara.PInvoke [PInvokeData("dwmapi.h")] 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); - if (!(typeof(T) == typeof(RECT) || isBool || typeof(T) == typeof(int) || typeof(T).IsEnum)) throw new ArgumentException(); var m = new SafeCoTaskMemHandle(Marshal.SizeOf(isBool ? typeof(uint) : typeof(T))); var hr = DwmGetWindowAttribute(hwnd, dwAttribute, (IntPtr)m, m.Size); pvAttribute = isBool ? (T)Convert.ChangeType(m.ToStructure(), typeof(bool)) : m.ToStructure(); @@ -617,7 +617,7 @@ namespace Vanara.PInvoke [PInvokeData("dwmapi.h")] public static HRESULT DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, [In] T pvAttribute) { - if (pvAttribute == null || !(pvAttribute is bool || pvAttribute is int || typeof(T).IsEnum)) throw new ArgumentException(); + if (pvAttribute == null || !CorrespondingTypeAttribute.CanSet(dwAttribute, typeof(T))) throw new ArgumentException(); var attr = pvAttribute is bool ? (object)Convert.ToUInt32(pvAttribute) : pvAttribute; using (var p = new PinnedObject(attr)) return DwmSetWindowAttribute(hwnd, dwAttribute, p, Marshal.SizeOf(attr));