Get Windows plans / power plans in C # (using WinAPI)

I am interested in getting all the power plans that you have on your computer using C #.

I thought you could use the PowerEnumerate API in some way :

DWORD WINAPI PowerEnumerate( _In_opt_ HKEY RootPowerKey, _In_opt_ const GUID *SchemeGuid, _In_opt_ const GUID *SubGroupOfPowerSettingsGuid, _In_ POWER_DATA_ACCESSOR AccessFlags, _In_ ULONG Index, _Out_opt_ UCHAR *Buffer, _Inout_ DWORD *BufferSize ); 

But I have no idea how much I really don’t know C. So, as I like, list all the available power plans and create a list of them. Then I want to have access to each GUID and their "convenient name".

So .. Maybe if someone who uses C #'s WinAPI well who would like to help, that would be great - or if someone has a better solution. I really tried to find a good answer to this question, but it does not seem to exist. I think that would help a lot of people.

Can anyone help with this?

+5
source share
1 answer

This should do it:

 using System; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Collections.Generic; public class Program { [DllImport("PowrProf.dll")] public static extern UInt32 PowerEnumerate(IntPtr RootPowerKey, IntPtr SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, UInt32 AcessFlags, UInt32 Index, ref Guid Buffer, ref UInt32 BufferSize); [DllImport("PowrProf.dll")] public static extern UInt32 PowerReadFriendlyName(IntPtr RootPowerKey, ref Guid SchemeGuid, IntPtr SubGroupOfPowerSettingGuid, IntPtr PowerSettingGuid, IntPtr Buffer, ref UInt32 BufferSize); public enum AccessFlags : uint { ACCESS_SCHEME = 16, ACCESS_SUBGROUP = 17, ACCESS_INDIVIDUAL_SETTING = 18 } private static string ReadFriendlyName(Guid schemeGuid) { uint sizeName = 1024; IntPtr pSizeName = Marshal.AllocHGlobal((int)sizeName); string friendlyName; try { PowerReadFriendlyName(IntPtr.Zero, ref schemeGuid, IntPtr.Zero, IntPtr.Zero, pSizeName, ref sizeName); friendlyName = Marshal.PtrToStringUni(pSizeName); } finally { Marshal.FreeHGlobal(pSizeName); } return friendlyName; } public static IEnumerable<Guid> GetAll() { var schemeGuid = Guid.Empty; uint sizeSchemeGuid = (uint)Marshal.SizeOf(typeof(Guid)); uint schemeIndex = 0; while (PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)AccessFlags.ACCESS_SCHEME, schemeIndex, ref schemeGuid, ref sizeSchemeGuid) == 0) { yield return schemeGuid; schemeIndex++; } } public static void Main() { var guidPlans = GetAll(); foreach (Guid guidPlan in guidPlans) { Console.WriteLine(ReadFriendlyName(guidPlan)); } } } 

You may need to run this program as an administrator for security purposes.

+6
source

Source: https://habr.com/ru/post/1204377/


All Articles