-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
chore: Migrate Win32 API relating code to use CsWin32 based code #3224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a0f1f19
ff6da03
f3d8e22
68950b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,64 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using System.Runtime.Versioning; | ||
| using Windows.Win32; | ||
| using Windows.Win32.Foundation; | ||
|
|
||
| namespace BenchmarkDotNet.Helpers | ||
| { | ||
| [SuppressMessage("ReSharper", "InconsistentNaming")] | ||
| [SupportedOSPlatform("windows6.0.6000")] | ||
| internal class PowerManagementHelper | ||
| { | ||
| private const uint ErrorMoreData = 234; | ||
| private const uint SuccessCode = 0; | ||
|
|
||
| internal static Guid? CurrentPlan | ||
| internal static unsafe Guid? CurrentPlan | ||
| { | ||
| get | ||
| { | ||
| IntPtr activeGuidPtr = IntPtr.Zero; | ||
| uint res = PowerGetActiveScheme(IntPtr.Zero, ref activeGuidPtr); | ||
| if (res != SuccessCode) | ||
| WIN32_ERROR res = PInvoke.PowerGetActiveScheme(null, out var activePolicyGuidPtr); | ||
| if (res != WIN32_ERROR.NO_ERROR) | ||
| return null; | ||
|
|
||
| return Marshal.PtrToStructure<Guid>(activeGuidPtr); | ||
| var activePolicyGuid = *activePolicyGuidPtr; | ||
|
|
||
| PInvoke.LocalFree((HLOCAL)activePolicyGuidPtr); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added |
||
|
|
||
| return activePolicyGuid; | ||
| } | ||
| } | ||
|
|
||
| internal static string CurrentPlanFriendlyName | ||
| internal unsafe static string CurrentPlanFriendlyName | ||
| { | ||
| get | ||
| { | ||
| uint buffSize = 0; | ||
| StringBuilder buffer = new StringBuilder(); | ||
| IntPtr activeGuidPtr = IntPtr.Zero; | ||
| uint res = PowerGetActiveScheme(IntPtr.Zero, ref activeGuidPtr); | ||
| if (res != SuccessCode) | ||
| WIN32_ERROR res = PInvoke.PowerGetActiveScheme(null, out var activeGuidPtr); | ||
| if (res != WIN32_ERROR.NO_ERROR) | ||
| return ""; | ||
| res = PowerReadFriendlyName(IntPtr.Zero, activeGuidPtr, IntPtr.Zero, IntPtr.Zero, buffer, ref buffSize); | ||
| if (res == ErrorMoreData) | ||
|
|
||
| Guid activeSchemaGuid = *activeGuidPtr; | ||
| PInvoke.LocalFree((HLOCAL)activeGuidPtr); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add LocalFree API call here |
||
|
|
||
| Span<byte> buffer = stackalloc byte[260]; | ||
| uint bufferSize = (uint)buffer.Length; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Original code don't set buffSize. |
||
|
|
||
| res = PInvoke.PowerReadFriendlyName(null, activeSchemaGuid, null, null, buffer, ref bufferSize); | ||
| if (res == WIN32_ERROR.ERROR_MORE_DATA) | ||
| { | ||
| buffer.Capacity = (int)buffSize; | ||
| res = PowerReadFriendlyName(IntPtr.Zero, activeGuidPtr, | ||
| IntPtr.Zero, IntPtr.Zero, buffer, ref buffSize); | ||
| buffer = new byte[(int)bufferSize]; | ||
| res = PInvoke.PowerReadFriendlyName(null, activeSchemaGuid, null, null, buffer, ref bufferSize); | ||
| bufferSize = (uint)buffer.Length; | ||
| } | ||
| if (res != SuccessCode) | ||
|
|
||
| if (res != WIN32_ERROR.NO_ERROR) | ||
| return ""; | ||
|
|
||
| return buffer.ToString(); | ||
| ReadOnlySpan<char> chars = MemoryMarshal.Cast<byte, char>(buffer.Slice(0, (int)bufferSize)); | ||
| return chars.TrimEnd('\0').ToString(); // Trim null terminator of PWSTR. | ||
| } | ||
| } | ||
|
|
||
| internal static bool Set(Guid newPolicy) | ||
| { | ||
| return PowerSetActiveScheme(IntPtr.Zero, ref newPolicy) == 0; | ||
| return PInvoke.PowerSetActiveScheme(null, newPolicy) == WIN32_ERROR.NO_ERROR; | ||
| } | ||
|
|
||
| [DllImport("powrprof.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] | ||
| private static extern uint PowerReadFriendlyName(IntPtr RootPowerKey, IntPtr SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, IntPtr PowerSettingGuid, StringBuilder Buffer, ref uint BufferSize); | ||
|
|
||
| [DllImport("powrprof.dll", ExactSpelling = true)] | ||
| private static extern int PowerSetActiveScheme(IntPtr ReservedZero, ref Guid policyGuid); | ||
|
|
||
| [DllImport("powrprof.dll", ExactSpelling = true)] | ||
| private static extern uint PowerGetActiveScheme(IntPtr UserRootPowerKey, ref IntPtr ActivePolicyGuid); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "$schema": "https://aka.ms/CsWin32.schema.json", | ||
| "allowMarshaling": false, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NativeAot requires this setting to be false. (Default: true) |
||
| "multiTargetingFriendlyAPIs": true | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| PowerGetActiveScheme | ||
| PowerSetActiveScheme | ||
| PowerReadFriendlyName | ||
| GetConsoleWindow | ||
| GetConsoleMode | ||
| GetStdHandle | ||
| SetConsoleMode | ||
| CloseHandle | ||
| PowerCreateRequest | ||
| PowerClearRequest | ||
| PowerSetRequest | ||
| PowerGetActiveScheme | ||
| LocalFree | ||
| GlobalMemoryStatusEx |
Uh oh!
There was an error while loading. Please reload this page.