Added nullability to PowrProf

nullableenabled
David Hall 2023-09-16 14:00:07 -06:00
parent 1df1439cfb
commit 5572f7429a
5 changed files with 207 additions and 230 deletions

View File

@ -1,7 +1,3 @@
using System;
using System.Runtime.InteropServices;
using Vanara.InteropServices;
namespace Vanara.PInvoke; namespace Vanara.PInvoke;
/// <summary>Functions, structures and constants from powrprof.dll.</summary> /// <summary>Functions, structures and constants from powrprof.dll.</summary>

View File

@ -1,7 +1,3 @@
using System;
using System.Runtime.InteropServices;
using Vanara.InteropServices;
namespace Vanara.PInvoke; namespace Vanara.PInvoke;
public static partial class PowrProf public static partial class PowrProf

View File

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Vanara.InteropServices;
namespace Vanara.PInvoke; namespace Vanara.PInvoke;
@ -3656,21 +3652,20 @@ public static partial class PowrProf
return f(g1.HasValue ? (IntPtr)(void*)&ptrs[0] : IntPtr.Zero, g2.HasValue ? (IntPtr)(void*)&ptrs[1] : IntPtr.Zero, g3.HasValue ? (IntPtr)(void*)&ptrs[2] : IntPtr.Zero); return f(g1.HasValue ? (IntPtr)(void*)&ptrs[0] : IntPtr.Zero, g2.HasValue ? (IntPtr)(void*)&ptrs[1] : IntPtr.Zero, g3.HasValue ? (IntPtr)(void*)&ptrs[2] : IntPtr.Zero);
} }
private static SafeHGlobalHandle PwrReadMem(PwrReadMemFunc f, Guid? g1, Guid? g2, Guid? g3) private static SafeHGlobalHandle PwrReadMem(PwrReadMemFunc f, Guid? g1, Guid? g2, Guid? g3) =>
PwrGuidTsl(g1, g2, g3, (p1, p2, p3) =>
{ {
return PwrGuidTsl(g1, g2, g3, (p1, p2, p3) => {
var sz = 0U; var sz = 0U;
var err = f(HKEY.NULL, p1, p2, p3, IntPtr.Zero, ref sz); var err = f(HKEY.NULL, p1, p2, p3, IntPtr.Zero, ref sz);
if (err.Failed) if (err.Failed)
{ {
if (err == Win32Error.ERROR_FILE_NOT_FOUND) return null; if (err == Win32Error.ERROR_FILE_NOT_FOUND) return SafeHGlobalHandle.Null;
if (err != Win32Error.ERROR_MORE_DATA) throw err.GetException(); if (err != Win32Error.ERROR_MORE_DATA) throw err.GetException()!;
} }
var p = new SafeHGlobalHandle((int)sz); var p = new SafeHGlobalHandle((int)sz);
f(HKEY.NULL, p1, p2, p3, (IntPtr)p, ref sz).ThrowIfFailed(); f(HKEY.NULL, p1, p2, p3, (IntPtr)p, ref sz).ThrowIfFailed();
return p; return p;
}); });
}
/// <summary>Contains parameters used when registering for a power notification.</summary> /// <summary>Contains parameters used when registering for a power notification.</summary>
// https://docs.microsoft.com/en-us/windows/desktop/api/powrprof/ns-powrprof-device_notify_subscribe_parameters typedef struct // https://docs.microsoft.com/en-us/windows/desktop/api/powrprof/ns-powrprof-device_notify_subscribe_parameters typedef struct

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,6 @@
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Vanara.Extensions;
using static Vanara.PInvoke.PowrProf; using static Vanara.PInvoke.PowrProf;
namespace Vanara.PInvoke.Tests; namespace Vanara.PInvoke.Tests;
@ -19,7 +13,7 @@ public class PowrProfTests
{ {
Assert.That(EnumPwrSchemes(p), Is.True); Assert.That(EnumPwrSchemes(p), Is.True);
bool p(uint uiIndex, uint dwName, string sName, uint dwDesc, string sDesc, in POWER_POLICY pp, IntPtr lParam) static bool p(uint uiIndex, uint dwName, string sName, uint dwDesc, string sDesc, in POWER_POLICY pp, IntPtr lParam)
{ {
TestContext.WriteLine($"Idx:{uiIndex}; Name:{sName}; Desc:{sDesc}"); TestContext.WriteLine($"Idx:{uiIndex}; Name:{sName}; Desc:{sDesc}");
return true; return true;
@ -45,13 +39,13 @@ public class PowrProfTests
} }
} }
void WriteName(Guid? sch, Guid? grp, Guid? setting) { TestContext.WriteLine($"{PowerReadFriendlyName(sch, grp, setting)} : {PowerReadDescription(sch, grp, setting)} : {setting}"); } static void WriteName(Guid? sch, Guid? grp, Guid? setting) => TestContext.WriteLine($"{PowerReadFriendlyName(sch, grp, setting)} : {PowerReadDescription(sch, grp, setting)} : {setting}");
} }
[Test] [Test]
public void PowerReadSettingAttributesTest() public void PowerReadSettingAttributesTest()
{ {
var attr = PowerReadSettingAttributes(default(Guid), default(Guid)); var attr = PowerReadSettingAttributes(default, default);
TestContext.WriteLine($"DefGuid={attr}"); TestContext.WriteLine($"DefGuid={attr}");
attr = PowerReadSettingAttributes(GUID_SYSTEM_BUTTON_SUBGROUP, default); attr = PowerReadSettingAttributes(GUID_SYSTEM_BUTTON_SUBGROUP, default);
TestContext.WriteLine($"GUID_SYSTEM_BUTTON_SUBGROUP={attr}"); TestContext.WriteLine($"GUID_SYSTEM_BUTTON_SUBGROUP={attr}");
@ -61,7 +55,7 @@ public class PowrProfTests
public void PowerSettingRegisterNotificationTest() public void PowerSettingRegisterNotificationTest()
{ {
uint timeOut = 0; uint timeOut = 0;
var evt = new AutoResetEvent(false); AutoResetEvent evt = new(false);
Assert.That(PowerSettingRegisterNotification(GUID_VIDEO_POWERDOWN_TIMEOUT, DEVICE_NOTIFY.DEVICE_NOTIFY_CALLBACK, new DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS { Callback = PowerSettingFunc }, out var powerNotification), ResultIs.Successful); Assert.That(PowerSettingRegisterNotification(GUID_VIDEO_POWERDOWN_TIMEOUT, DEVICE_NOTIFY.DEVICE_NOTIFY_CALLBACK, new DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS { Callback = PowerSettingFunc }, out var powerNotification), ResultIs.Successful);
evt.WaitOne(1000); evt.WaitOne(1000);
Assert.That(PowerSettingUnregisterNotification(powerNotification), ResultIs.Successful); Assert.That(PowerSettingUnregisterNotification(powerNotification), ResultIs.Successful);