コード例 #1
0
 public static extern uint CallNtPowerInformation(
     PowerInformationLevel InformationLevel,
     IntPtr lpInputBuffer,
     int nInputBufferSize,
     ref SystemBatteryState lpOutputBuffer,
     int nOutputBufferSize
     );
 public static extern uint CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     IntPtr inputBuffer,
     uint inputBufferSize,
     out SystemPowerCapabilities outputBuffer,
     uint outputBufferSize
     );
コード例 #3
0
 internal static extern UInt32 CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     IntPtr inputBuffer,
     UInt32 inputBufferSize,
     out SystemPowerCapabilities outputBuffer,
     UInt32 outputBufferSize
     );
コード例 #4
0
        private void SetPowerInformationBooleanValue(PowerInformationLevel level, bool value)
        {
            var inputBuffer = AllocateCoTaskBuffer <bool>(out var inputBufferSize);

            var byteValue = value ? (byte)1 : (byte)0;

            Marshal.WriteByte(inputBuffer, ofs: 0, val: byteValue);

            try
            {
                var returnCode = PowrprofDllImport
                                 .CallNtPowerInformation(
                    level,
                    inputBuffer,
                    (ulong)inputBufferSize,
                    IntPtr.Zero,
                    0);

                EnsureWin32CallSucceeded(returnCode);
            }
            finally
            {
                Marshal.FreeCoTaskMem(inputBuffer);
            }
        }
 public static extern uint CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     IntPtr inputBuffer,
     uint inputBufferSize,
     out SystemBatteryState outputBuffer,
     uint outputBufferSize
     );
コード例 #6
0
        private T GetStructure <T>(PowerInformationLevel informationLevel)
        {
            IntPtr lpInBuffer    = IntPtr.Zero;
            int    inputBufSize  = 0;
            int    outputPtrSize = _marshal.SizeOf <T>();
            IntPtr outputPtr     = _marshal.AllocateMemory(size: outputPtrSize);

            PointerResult result = _powerManagementInterop.CallNtPowerInformation(
                informationLevel: informationLevel,
                inputBuffer: lpInBuffer,
                inputBufSize: inputBufSize,
                outputBuffer: outputPtr,
                outputBufferSize: outputPtrSize);

            _marshal.ReleasePointer(lpInBuffer);

            if (result.IsSuccessful)
            {
                T properties = _marshal.ToProperties <T>(outputPtr);
                _marshal.ReleasePointer(outputPtr);
                return(properties);
            }
            else
            {
                _marshal.ReleasePointer(outputPtr);
                throw new Win32Exception();
            }
        }
コード例 #7
0
 internal static extern UInt32 CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     IntPtr inputBuffer,
     UInt32 inputBufferSize,
     out SystemBatteryState outputBuffer,
     UInt32 outputBufferSize
     );
コード例 #8
0
        internal static T GetPowerInformation <T>(PowerInformationLevel informationLevel)
        {
            IntPtr powerInformation = IntPtr.Zero;

            try
            {
                powerInformation = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(T)));

                int ntStatus = CallNtPowerInformation(informationLevel, IntPtr.Zero, 0, powerInformation,
                                                      (uint)Marshal.SizeOf(typeof(T)));

                if (ntStatus != 0)
                {
                    return(default(T));
                }

                // receives a ULONGLONG that specifies the interrupt-time count, in 100-nanosecond units, at the last system sleep time
                // there are 1e9 nanoseconds in a second, so there are 1e7 100 - nanoseconds in a second

                //long lastSleepTimeInSeconds = Marshal.ReadInt64(informationTime, 0) / 10000000;


                return((T)ProcessResult <T>(powerInformation));
            }
            finally
            {
                if (powerInformation != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(powerInformation);
                }
            }
        }
コード例 #9
0
 private static extern int CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     IntPtr lpInputBuffer,
     int nInputBufferSize,
     out ExecutionState state,
     int nOutputBufferSize
     );
コード例 #10
0
        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();
            }
        }
コード例 #11
0
 public static extern uint CallNtPowerInformation(
     PowerInformationLevel InformationLevel,
     IntPtr lpInputBuffer,
     int nInputBufferSize,
     out SystemPowerInformation spi,
     int nOutputBufferSize
     );
コード例 #12
0
        /// <summary>
        /// Gets a last time of the system change a power state.
        /// </summary>
        /// <param name="level">Indicates power level information.</param>
        /// <returns>Last time of the changing the system power state.</returns>
        private static DateTime LastPowerChangedStateTime(PowerInformationLevel level)
        {
            var lastWakeDuration = GetPowerInformation <long>(level);

            var lastSleepTime = DateTime.Now - TimeSpan.FromMilliseconds(PowerManagementNative.GetTickCount64()) +
                                TimeSpan.FromTicks(lastWakeDuration);

            return(lastSleepTime);
        }
コード例 #13
0
        /// <summary>
        /// Gets System Power Information by PowerInformation level.
        /// </summary>
        /// <typeparam name="T">An output buffer type.</typeparam>
        /// <param name="powerLevel">Indicates power level information.</param>
        /// <returns>The output buffer result.</returns>
        private static T GetPowerInformation <T>(PowerInformationLevel powerLevel)
        {
            T powerInformation = default(T);

            InteropPowerManagment.CallNtPowerInformationForRead <SystemBatteryState>(
                powerLevel,
                buffer => powerInformation = Marshal.PtrToStructure <T>(buffer));

            return(powerInformation);
        }
コード例 #14
0
        private int GetSleepOrWakeTime(PowerInformationLevel informationLevel)
        {
            var result = PowerManagementConfiguration.CallNtPowerInformation(informationLevel, IntPtr.Zero, 0,
                                                                             out long time, Marshal.SizeOf <long>());

            var dateTime = new DateTime(2020, 4, 13, 0, 0, 0);

            return(result == PowerInformationStatus.Success
                ? dateTime.AddTicks(time / 100).Second
                : throw new Win32Exception("Something went wrong."));
        }
コード例 #15
0
        private long GetPowerInformation(PowerInformationLevel informationLevel)
        {
            var result = PowerManagementNative.CallNtPowerInformation(
                informationLevel,
                IntPtr.Zero,
                0,
                out IntPtr time,
                (uint)Marshal.SizeOf(typeof(long)));

            return(this.IsSuccess(result) ? time.ToInt64() : 0);
        }
コード例 #16
0
        /// <summary>
        /// Wraps CallNtPowerInformationForRead method.
        /// </summary>
        /// <typeparam name="T">An output buffer type.</typeparam>
        /// <param name="level">Indicates power level information.</param>
        /// <param name="readOutputBuffer">The output buffer result.</param>
        public static void CallNtPowerInformationForRead <T>(PowerInformationLevel level, Action <IntPtr> readOutputBuffer)
        {
            var outputBufferSize = Marshal.SizeOf <T>();
            var outputBuffer     = Marshal.AllocHGlobal(outputBufferSize);

            PowerManagementNative.CallNtPowerInformation((int)level, IntPtr.Zero, 0, outputBuffer,
                                                         (uint)outputBufferSize);
            readOutputBuffer.Invoke(outputBuffer);

            Marshal.FreeHGlobal(outputBuffer);
        }
コード例 #17
0
        /// <summary>
        /// Wraps CallNtPowerInformationForRead method.
        /// </summary>
        /// <typeparam name="T">An input buffer type.</typeparam>
        /// <param name="level">Indicates power level information.</param>
        /// <param name="writeInputBuffer">The function that is responsible for marhaling input parametrs.</param>
        public static void CallNtPowerInformationForWrite <T>(PowerInformationLevel level, Action <IntPtr> writeInputBuffer)
        {
            var inputBufferSize = Marshal.SizeOf <T>();
            var inputBuffer     = Marshal.AllocHGlobal(inputBufferSize);

            writeInputBuffer.Invoke(inputBuffer);

            PowerManagementNative.CallNtPowerInformation((int)level, inputBuffer,
                                                         (uint)inputBufferSize, IntPtr.Zero, 0);

            Marshal.FreeHGlobal(inputBuffer);
        }
コード例 #18
0
        private T GetSystemInformation <T>(PowerInformationLevel level)
        {
            int    outputBufferSize = Marshal.SizeOf(typeof(T));
            IntPtr outputBuffer     = Marshal.AllocHGlobal(outputBufferSize);

            CallNtPowerInformation((int)level, IntPtr.Zero, 0, outputBuffer, (uint)outputBufferSize);

            var powerInformation = Marshal.PtrToStructure <T>(outputBuffer);

            Marshal.FreeHGlobal(outputBuffer);

            return(powerInformation);
        }
コード例 #19
0
 public PointerResult CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     IntPtr inputBuffer,
     int inputBufSize,
     IntPtr outputBuffer,
     int outputBufferSize)
 {
     return(new PointerResult(PowerManagementInterop.CallNtPowerInformation(
                                  informaitonLevel: (int)informationLevel,
                                  inputBuffer: inputBuffer,
                                  inputBufSize: inputBufSize,
                                  outputBuffer: outputBuffer,
                                  outputBufferSize: outputBufferSize)));
 }
コード例 #20
0
        public PointerResult CallNtPowerInformation(
            PowerInformationLevel informationLevel,
            IntPtr lpInputBuffer,
            int inputBufSize,
            ProcessorPowerInformation[] lpOutputBuffer,
            int nOutputBufferSize)
        {
            var result = PowerManagementInterop.CallNtPowerInformation(
                (int)informationLevel,
                lpInputBuffer,
                inputBufSize,
                lpOutputBuffer,
                nOutputBufferSize);

            return(new PointerResult(result));
        }
コード例 #21
0
        private DateTime GetLastTime(PowerInformationLevel level)
        {
            int    outputBufferSize = Marshal.SizeOf(typeof(ulong));
            IntPtr outputBuffer     = Marshal.AllocHGlobal(outputBufferSize);

            CallNtPowerInformation((int)level, IntPtr.Zero, 0, outputBuffer, (uint)outputBufferSize);

            // The interrupt-time count, in 100-nanosecond units
            long ticksCount = Marshal.ReadInt64(outputBuffer);

            Marshal.FreeHGlobal(outputBuffer);
            var systemStartupTime = GetTickCount64() * 1000000 / 100;

            var date = DateTime.UtcNow - TimeSpan.FromTicks((long)systemStartupTime) + TimeSpan.FromTicks(ticksCount);

            return(date.ToLocalTime());
        }
コード例 #22
0
        private IntPtr CallNtPowerInfo <T>(PowerInformationLevel level) where T : struct
        {
            int    size           = Marshal.SizeOf <T>();
            IntPtr lpOutputBuffer = Marshal.AllocHGlobal(size);

            try
            {
                uint status = CallNtPowerInformation((int)level, IntPtr.Zero, 0, lpOutputBuffer, (uint)size);

                return(status == 0 ? lpOutputBuffer : IntPtr.Zero);
            }
            finally
            {
                if (lpOutputBuffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(lpOutputBuffer);
                }
            }
        }
コード例 #23
0
        private T GetPowerInformationStructure <T>(PowerInformationLevel level)
            where T : struct
        {
            var outputBuffer = AllocateCoTaskBuffer <T>(out var outputBufferSize);

            try
            {
                var returnCode = PowrprofDllImport.CallNtPowerInformation(
                    level,
                    (IntPtr)null,
                    0,
                    outputBuffer,
                    (ulong)outputBufferSize);
                EnsureWin32CallSucceeded(returnCode);

                return(Marshal.PtrToStructure <T>(outputBuffer));
            }
            finally
            {
                Marshal.FreeCoTaskMem(outputBuffer);
            }
        }
コード例 #24
0
        private long GetPowerInformationInt64Value(PowerInformationLevel level)
        {
            var outputBuffer = AllocateCoTaskBuffer <long>(out var outputBufferSize);

            try
            {
                // Result returned in count of 100 nanoseconds
                var returnCode = Dlls.CallNtPowerInformation(
                    15,
                    IntPtr.Zero,
                    0,
                    outputBuffer,
                    24);
                EnsureWin32CallSucceeded(returnCode);

                return(Marshal.ReadInt64(outputBuffer, 0));
            }
            finally
            {
                Marshal.FreeCoTaskMem(outputBuffer);
            }
        }
コード例 #25
0
 public static extern PowerInformationStatus CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     IntPtr inputBuffer,
     int inputBufferSize,
     IntPtr outputBuffer,
     uint outputBufferSize);
コード例 #26
0
 public static extern uint CallNtPowerInformation(
     [In] PowerInformationLevel informationLevel,
     [In] IntPtr lpInputBuffer,
     ulong nInputBufferSize,
     [Out] IntPtr lpOutputBuffer,
     ulong nOutputBufferSize);
コード例 #27
0
 private static extern int CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     [In] IntPtr lpInputBuffer,
     uint nInputBufferSize,
     [In, Out] IntPtr lpOutputBuffer,
     uint nOutputBufferSize);
コード例 #28
0
 internal static extern UInt32 CallNtPowerInformation(
      PowerInformationLevel informationLevel,
      IntPtr inputBuffer,
      UInt32 inputBufferSize,
      out SystemBatteryState outputBuffer,
      UInt32 outputBufferSize
 );
コード例 #29
0
 private static extern NtStatus CallNtPowerInformation(
     PowerInformationLevel informationLevel, 
     ref bool inputBuffer, 
     int inputBufferSize, 
     IntPtr outputBuffer, 
     int outputBufferSize);
コード例 #30
0
 internal static extern UInt32 CallNtPowerInformation(
      PowerInformationLevel informationLevel,
      IntPtr inputBuffer,
      UInt32 inputBufferSize,
      out SystemPowerCapabilities outputBuffer,
      UInt32 outputBufferSize
 );
コード例 #31
0
 internal static extern uint CallNtPowerInformation(PowerInformationLevel informationLevel,
                                                    IntPtr inputBuffer, int onputBufferSize,
                                                    IntPtr outputResult, int outputBufferSize);
コード例 #32
0
 internal static extern uint CallNtPowerInformation(PowerInformationLevel informationLevel,
                                                    IntPtr inputBuffer, int inputBufferSize,
                                                    out SystemPowerInformation outputBuffer, int outputBufferSize);
コード例 #33
0
 private static extern NtStatus CallNtPowerInformation(
     PowerInformationLevel informationLevel, 
     IntPtr inputBuffer, 
     int inputBufferSize, 
     out SystemBatteryState outputBuffer, 
     int outputBufferSize);
コード例 #34
0
 public static extern PowerInformationStatus CallNtPowerInformation(
     PowerInformationLevel informationLevel,
     IntPtr inputBuffer,
     int inputBufferSize,
     out SystemPowerInformation systemBatteryState,
     int outputBufferSize);