Ejemplo n.º 1
0
        private static bool TryGetDevice(uint index, out string result, QueryInterpretationFlags?flags = null)
        {
            uint size = 0;
            QueryInterpretationFlags targetFlags = flags ?? flagsAll;

            PowerManagement.DevicePowerEnumDevices(index, targetFlags, flagsQuery, IntPtr.Zero, ref size);

            if (size > 0)
            {
                var buffer = Marshal.AllocHGlobal((int)size);

                try
                {
                    if (PowerManagement.DevicePowerEnumDevices(index, targetFlags, flagsQuery, buffer, ref size) && buffer != IntPtr.Zero)
                    {
                        result = Marshal.PtrToStringUni(buffer);
                        return(true);
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }

            result = null;
            return(false);
        }