private T GetPowerInformation <T>(PowerInformationLevel informaitonLevel)
        {
            IntPtr inputBuffer        = IntPtr.Zero;
            int    inputBufferLength  = 0;
            int    outputBufferLength = Marshal.SizeOf <T>();
            IntPtr outputBuffer       = Marshal.AllocCoTaskMem(outputBufferLength);

            var retval = PowerManagementInteroperation.CallNtPowerInformation(
                (int)informaitonLevel,
                inputBuffer,
                inputBufferLength,
                outputBuffer,
                outputBufferLength);

            Marshal.FreeHGlobal(inputBuffer);
            if (retval == PowerManagementInteroperation.STATUS_SUCCESS)
            {
                var obj = Marshal.PtrToStructure <T>(outputBuffer);
                Marshal.FreeHGlobal(outputBuffer);
                return(obj);
            }
            else
            {
                throw new Win32Exception();
            }
        }
        public void SetSuspendState(bool hibernate, bool force, bool wakeupEventsDisabled)
        {
            var result = PowerManagementInteroperation.SetSuspendState(hibernate, force, wakeupEventsDisabled);

            if (!result)
            {
                throw new Win32Exception();
            }
        }
Ejemplo n.º 3
0
        private void ExecuteHibernateFileAction(HibernateFileAction fileAction)
        {
            int    inputBufferLength = Marshal.SizeOf <bool>();
            IntPtr inputBuffer       = Marshal.AllocCoTaskMem(inputBufferLength);

            Marshal.WriteByte(inputBuffer, (byte)fileAction);

            var retval = PowerManagementInteroperation.CallNtPowerInformation(
                (int)PowerInformationLevel.SystemReserveHiberFile,
                inputBuffer,
                inputBufferLength,
                IntPtr.Zero,
                0);

            Marshal.FreeHGlobal(inputBuffer);
            if (retval == PowerManagementInteroperation.STATUS_SUCCESS)
            {
            }
            else
            {
                throw new Win32Exception();
            }
        }