PowerEnumerate (powrprof)
Last changed: -111.225.3.197

.
Summary
Enumerates the specified elements in a power scheme. This function is normally called in a loop incrementing the Index parameter to retrieve subkeys until they've all been enumerated.

C# Signature:

[DllImport("powrprof.dll", SetLastError=true)]
static extern uint PowerEnumerate(IntPtr RootPowerKey,
                     IntPtr SchemeGuid,
                     IntPtr SubGroupOfPowerSettingsGuid,
                     PowerDataAccessor AccessFlags,
                     uint Index,
                     IntPtr Buffer,
                     ref uint BufferSize);

enum PowerDataAccessor : uint
{
   ACCESS_AC_POWER_SETTING_INDEX = 0,
   ACCESS_DC_POWER_SETTING_INDEX = 1,
   ACCESS_SCHEME = 16,
   ACCESS_SUBGROUP = 17,
   ACCESS_INDIVIDUAL_SETTING = 18,
   ACCESS_ACTIVE_SCHEME = 19,
   ACCESS_CREATE_SCHEME = 20
}

VB Signature:

Declare Function PowerEnumerate Lib "powrprof.dll" (RootPowerKey As IntPtr,
                SchemeGuid As IntPtr,
                SubGroupOfPowerSettingsGuid As IntPtr,
                AccessFlags As PowerDataAccessor,
                Index As UInteger,
                Buffer As IntPtr,
                ByRef BufferSize As UInteger) As UInteger

Public Enum PowerDataAccessor As UInteger
     ACCESS_AC_POWER_SETTING_INDEX = 0
     ACCESS_DC_POWER_SETTING_INDEX = 1
     ACCESS_SCHEME = 16
     ACCESS_SUBGROUP = 17
     ACCESS_INDIVIDUAL_SETTING = 18
     ACCESS_ACTIVE_SCHEME = 19
     ACCESS_CREATE_SCHEME = 20
End Enum

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Use EnumPwrSchemes for Windows versions prior to Vista.

Tips & Tricks:

Please add some!

Sample Code:

List<Guid> GUIDs = new List<Guid>();

IntPtr ReadBuffer;
uint BufferSize = 16;

uint Index = 0;
uint ReturnCode = 0;

while (ReturnCode == 0)
{
   ReadBuffer = Marshal.AllocHGlobal((int)BufferSize);

   try
   {
     ReturnCode = PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, PowerDataAccessor.ACCESS_SCHEME, Index, ReadBuffer, ref BufferSize);

     if (ReturnCode == 259) break; // no more data
     if (ReturnCode != 0)
     {
       throw new COMException("Error occurred while enumerating power schemes. Win32 error code: " + ReturnCode);
     }

     Guid NewGuid = (Guid) Marshal.PtrToStructure(ReadBuffer, typeof(Guid));
     GUIDs.Add(NewGuid);
   }
   finally
   {
     Marshal.FreeHGlobal(ReadBuffer);
   }

   Index++;
}

// now we have a list of identifiers for all the power schemes on the system,
// we can pass those into other powrprof methods to retrieve more information on each

Documentation