PowerReadFriendlyName (powrprof)
Last changed: -14.97.250.110

.
Summary
Retreives friendly name for power scheme with given GUID

C# Signature:

     [DllImport("powrprof.dll")]
    public static extern UInt32 PowerReadFriendlyName(
        IntPtr RootPowerKey,
        IntPtr SchemeGuid,
        IntPtr SubGroupOfPowerSettingGuid,
        IntPtr PowerSettingGuid,
        IntPtr 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);

VB Signature:

Private Declare Function PowerReadFriendlyName Lib "powrprof.dll" Alias _
    "PowerReadFriendlyName" (RootPowerKey As IntPtr, ByRef SchemeGuid As Guid, _
                SubGroupOfPowerSettingGuid As IntPtr, PowerSettingGuid As IntPtr, _
                Buffer As IntPtr, ByRef BufferSize As UInteger) As UInteger

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Tips & Tricks:

    ''' <summary>
    ''' Retrieves the friendly name for the specified power setting, subgroup, or scheme, in Unicode (Each Char is 2 bytes).
    ''' </summary>
    ''' <param name="RootPowerKey">This parameter is reserved for future use and must be set to NULL</param>
    ''' <param name="SchemeGuid">The identifier of the power scheme.</param>
    ''' <param name="SubGroupOfPowerSettingGuid">The subgroup of power settings. Use NO_SUBGROUP_GUID to refer to the default power scheme.</param>
    ''' <param name="PowerSettingGuid">The identifier of the power setting that is being used.</param>
    ''' <param name="Buffer">A pointer to a buffer that receives the friendly name. If this parameter is NULL, the BufferSize parameter receives the required buffer size</param>
    ''' <param name="BufferSize">Size of the buffer.</param>
    ''' <returns>Returns ERROR_SUCCESS (zero) if the call was successful, and a non-zero value if the call failed.
    ''' If the buffer size passed in the BufferSize parameter is too small, or if the Buffer parameter is NULL,
    ''' ERROR_MORE_DATA will be returned and the DWORD pointed to by the BufferSize parameter will be filled in with
    ''' the required buffer size.</returns>

Sample Code:

    private string GetCurrentPowerSchemeVistaAPI()
    {
        IntPtr ptrActiveGuid = IntPtr.Zero;
        uint res = PowerGetActiveScheme(IntPtr.Zero, ref ptrActiveGuid);
        if (res == 0)
        {
        uint buffSize = 0;
        res = PowerReadFriendlyName(IntPtr.Zero, ptrActiveGuid, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref buffSize);
        if (res == 0)
        {
            if (buffSize == 0u) return "";
            IntPtr ptrName = Marshal.AllocHGlobal((int)buffSize);
            res = PowerReadFriendlyName(IntPtr.Zero, ptrActiveGuid, IntPtr.Zero, IntPtr.Zero, ptrName, ref buffSize);
            if (res == 0)
            {
            string ret = Marshal.PtrToStringUni(ptrName);
            Marshal.FreeHGlobal(ptrName);
            return ret;
            }
            Marshal.FreeHGlobal(ptrName);
        }
        }
        throw new Exception("Error reading current power scheme. Native Win32 error code = " + res);
    }

Documentation