Exemple #1
0
        public SubGroupNode(Guid schemeGuid, Guid subGroupGuid, PersistentSettings settings)
        {
            this.settings      = settings;
            this.schemeGuid    = schemeGuid;
            this.subGroup      = new SettingSubGroup();
            this.subGroup.Guid = subGroupGuid.ToString();

            IntPtr friendlyName       = Marshal.AllocHGlobal(1000);
            IntPtr powerSchemeGuidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid)));
            IntPtr groupGuidPtr       = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid)));
            uint   bufferSize         = 1000;

            try
            {
                Marshal.StructureToPtr(schemeGuid, powerSchemeGuidPtr, true);
                Marshal.StructureToPtr(subGroupGuid, groupGuidPtr, true);

                //Pass the guid retrieved in PowerEnumerate as
                //parameter to get the sub group name.
                NativeMethods.PowerReadFriendlyName(IntPtr.Zero,
                                                    powerSchemeGuidPtr, groupGuidPtr, IntPtr.Zero, friendlyName, ref bufferSize);

                string subGroupName = Marshal.PtrToStringUni(friendlyName);
                this.subGroup.Name = subGroupName;

                this.subGroup.Description = PowerManager.GetDescription(schemeGuid.ToString(), subGroup.Guid, null);
            }
            finally
            {
                Marshal.FreeHGlobal(powerSchemeGuidPtr);
                Marshal.FreeHGlobal(groupGuidPtr);
                Marshal.FreeHGlobal(friendlyName);
            }
        }
        public SchemeNode(Guid schemeGuid, PersistentSettings settings)
            : base()
        {
            this.settings         = settings;
            this.powerScheme      = new PowerScheme();
            this.powerScheme.Guid = schemeGuid.ToString();

            IntPtr ptrToPowerSchemeGuid = IntPtr.Zero;
            IntPtr friendlyName         = IntPtr.Zero;
            uint   buffSize             = 1000;

            try
            {
                friendlyName         = Marshal.AllocHGlobal((int)buffSize);
                ptrToPowerSchemeGuid = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid)));
                Marshal.StructureToPtr(schemeGuid, ptrToPowerSchemeGuid, true);

                //Pass the guid retrieved in PowerEnumerate as parameter
                //to get the power scheme name.
                NativeMethods.PowerReadFriendlyName(
                    IntPtr.Zero, ptrToPowerSchemeGuid, IntPtr.Zero, IntPtr.Zero,
                    friendlyName, ref buffSize);

                this.powerScheme.Name = Marshal.PtrToStringUni(friendlyName);

                this.powerScheme.Description = PowerManager.GetDescription(powerScheme.Guid, null, null);
            }
            catch (Exception exception)
            {
                throw new PowerManagerException(exception.Message, exception);
            }
            finally
            {
                Marshal.FreeHGlobal(ptrToPowerSchemeGuid);
                Marshal.FreeHGlobal(friendlyName);
            }
        }
        public SettingNode(Guid powerSchemeGuid, Guid subGroupGuid, Guid powerSettingGuid, PersistentSettings settings)
            : base()
        {
            this.settings        = settings;
            this.powerSchemeGuid = powerSchemeGuid;
            this.subGroupGuid    = subGroupGuid;

            uint   returnCode         = 0;
            IntPtr friendlyName       = IntPtr.Zero;
            IntPtr settingGuidPtr     = IntPtr.Zero;
            IntPtr subGroupGuidPtr    = IntPtr.Zero;
            IntPtr powerSchemeGuidPtr = IntPtr.Zero;

            try
            {
                friendlyName       = Marshal.AllocHGlobal(1000);
                settingGuidPtr     = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid)));
                subGroupGuidPtr    = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid)));
                powerSchemeGuidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid)));
                uint bufferSize = 1000;

                Marshal.StructureToPtr(powerSchemeGuid, powerSchemeGuidPtr, true);
                Marshal.StructureToPtr(subGroupGuid, subGroupGuidPtr, true);
                Marshal.StructureToPtr(powerSettingGuid, settingGuidPtr, true);

                //Get the power setting name.
                NativeMethods.PowerReadFriendlyName(
                    IntPtr.Zero, powerSchemeGuidPtr, subGroupGuidPtr, settingGuidPtr,
                    friendlyName, ref bufferSize);

                string settingName = Marshal.PtrToStringUni(friendlyName);

                powerSetting             = new PowerSetting();
                powerSetting.Name        = settingName;
                powerSetting.Guid        = powerSettingGuid.ToString();
                powerSetting.Description = PowerManager.GetDescription(powerSchemeGuid.ToString(), subGroupGuid.ToString(), powerSetting.Guid);

                returnCode = NativeMethods.PowerReadACValueIndex(
                    IntPtr.Zero, powerSchemeGuidPtr, subGroupGuidPtr, settingGuidPtr, ref acValue);

                returnCode = NativeMethods.PowerReadDCValueIndex(
                    IntPtr.Zero, powerSchemeGuidPtr, subGroupGuidPtr, settingGuidPtr, ref dcValue);

                isRange = NativeMethods.PowerIsSettingRangeDefined(subGroupGuidPtr, settingGuidPtr);

                // Bit mask !!!
                isHidden       = 1 == (NativeMethods.PowerReadSettingAttributes(subGroupGuidPtr, settingGuidPtr) & 1);
                base.IsVisible = !isHidden;

                if (isRange)
                {
                    ReadNonindexedValues(subGroupGuidPtr, settingGuidPtr);
                }
                else
                {
                    ReadIndexedValues(subGroupGuidPtr, settingGuidPtr);
                }
            }
            catch (Exception exception)
            {
                throw new PowerManagerException(exception.Message, exception);
            }
            finally
            {
                Marshal.FreeHGlobal(friendlyName);
                Marshal.FreeHGlobal(powerSchemeGuidPtr);
                Marshal.FreeHGlobal(subGroupGuidPtr);
                Marshal.FreeHGlobal(settingGuidPtr);
            }
        }