DevicePowerEnumDevices (powrprof)
Last changed: anonymous

.
Summary
Enumerates devices on the system that meet the specified criteria.

C# Signature:

[DllImport("powrprof.dll", SetLastError=true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DevicePowerEnumDevices(uint queryIndex, QueryInterpretationFlags queryInterpretationFlags, QueryFlags queryFlags, IntPtr pReturnBuffer, ref uint pBufferSize);

VB Signature:

Declare Function DevicePowerEnumDevices Lib "powrprof.dll" (TODO) As TODO

User-Defined Types:

QueryInterpretationFlags, QueryFlags

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

If pReturnBuffer is NULL, pBufferSize will be filled with the size needed to return the data.

Tips & Tricks:

Please add some!

Sample Code:

    const int MAX_PATH = 261;

    if (!DevicePowerOpen(0))
        return; // error

    uint size = MAX_PATH;
    IntPtr ptrBuffer = Marshal.AllocHGlobal((int)size);
    uint index = 0;
    bool result = false;
    try
    {
        while ((ptrBuffer != IntPtr.Zero) &&
            (result = DevicePowerEnumDevices(index, QueryInterpretationFlags.DEVICEPOWER_FILTER_DEVICES_PRESENT, QueryFlags.PDCAP_D2_SUPPORTED, ptrBuffer, ref size)) != false)
        {
            string strBuffer = Marshal.PtrToStringUni(ptrBuffer);
            string str = string.Format("Device name: {0}\n", strBuffer);

            index++;
        }
    }
    finally
    {
        Marshal.FreeHGlobal(ptrBuffer);

        DevicePowerClose();
    }

Documentation