Example #1
0
        /// <summary>
        /// Suspend standby by SetThreadExecutionSTate because of a given CheckSuspendResult.
        /// </summary>
        /// <param name="result">The result why the standby should be suspended.</param>
        private void SuspendStandbyThreadExecutionState(CheckSuspendResult result)
        {
            try
            {
                lock (lockObj)
                {
                    var success = NativeMethods.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);

                    if (success == 0)
                    {
                        this.standbySuspended = false;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Core.StandbyManager_SuspendStandbyFailed, ex);
                    }
                    else
                    {
                        this.standbySuspended = true;

                        if (!String.IsNullOrEmpty(result.Reason))
                        {
                            WsapmLog.Log.WriteLine(String.Format(Resources.Wsapm_Core.StandbyManager_StandbySuspendedWithReason, result.Reason), LogMode.Normal);
                        }
                        else
                        {
                            WsapmLog.Log.WriteLine(Resources.Wsapm_Core.StandbyManager_StandbySuspendedWithoutReason, LogMode.Verbose);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WsapmLog.Log.WriteError(ex);
            }
        }
Example #2
0
        /// <summary>
        /// Enable standby using ThreadExecutionState.
        /// </summary>
        private void EnableStandbyThreadExecutionState()
        {
            try
            {
                lock (lockObj)
                {
                    bool standbyWasSuspended = this.standbySuspended;
                    var  success             = NativeMethods.SetThreadExecutionState(ES_CONTINUOUS);

                    if (success == 0)
                    {
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Core.StandbyManager_StandbyEnableFailed, ex);
                    }

                    // Standby successfully enabled.
                    this.standbySuspended = false;

                    if (standbyWasSuspended)
                    {
                        WsapmLog.Log.WriteLine(Resources.Wsapm_Core.StandbyManager_StandbyEnabled, LogMode.Normal); // Only write log if standby was suspended before.
                    }
                }
            }
            catch (Exception ex)
            {
                WsapmLog.Log.WriteError(ex);
            }
        }
Example #3
0
        /// <summary>
        /// Suspend standby by PowerAvailabilityRequest because of a given CheckSuspendResult.
        /// </summary>
        /// <param name="result">The result why the standby should be suspended.</param>
        private void SuspendStandbyPowerAvailabilityRequest(CheckSuspendResult result)
        {
            try
            {
                lock (lockObj)
                {
                    // Clear current power request if there is any.
                    if (this.currentPowerRequest != IntPtr.Zero)
                    {
                        NativeMethods.PowerClearRequest(this.currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);
                        this.currentPowerRequest = IntPtr.Zero;
                        this.standbySuspended    = false;
                    }

                    POWER_REQUEST_CONTEXT pContext;
                    pContext.Flags              = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
                    pContext.Version            = POWER_REQUEST_CONTEXT_VERSION;
                    pContext.SimpleReasonString = Resources.Wsapm_Core.StandbyManager_StandbySuspended;

                    this.currentPowerRequest = NativeMethods.PowerCreateRequest(ref pContext);

                    if (this.currentPowerRequest == IntPtr.Zero)
                    {
                        this.standbySuspended = false;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Core.StandbyManager_CreatePowerRequestFail, ex);
                    }

                    bool success = NativeMethods.PowerSetRequest(this.currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);

                    if (!success)
                    {
                        this.standbySuspended    = false;
                        this.currentPowerRequest = IntPtr.Zero;
                        var ex = Win32ExceptionManager.GetWin32Exception();
                        throw new WsapmException(Resources.Wsapm_Core.StandbyManager_SuspendStandbyFailed, ex);
                    }
                    else
                    {
                        this.standbySuspended = true;

                        if (!String.IsNullOrEmpty(result.Reason))
                        {
                            WsapmLog.Log.WriteLine(String.Format(Resources.Wsapm_Core.StandbyManager_StandbySuspendedWithReason, result.Reason), LogMode.Normal);
                        }
                        else
                        {
                            WsapmLog.Log.WriteLine(Resources.Wsapm_Core.StandbyManager_StandbySuspendedWithoutReason, LogMode.Verbose);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WsapmLog.Log.WriteError(ex);
            }
        }
Example #4
0
        /// <summary>
        /// Gets a value indicating of wake timers are allowed.
        /// </summary>
        /// <returns>True, if wake timers are allowed, otherwise false.</returns>
        public static bool GetWakeTimersAllowed()
        {
            if (OsVersionTools.IsWindowsVistaOrLater())
            {
                bool   wakeTimersAllowed = false;
                IntPtr activeGuidPtr     = IntPtr.Zero;

                if (NativeMethods.PowerGetActiveScheme(IntPtr.Zero, ref activeGuidPtr) != 0)
                {
                    var ex = Win32ExceptionManager.GetWin32Exception();
                    throw ex;
                }

                Guid   activeSchemeGuid = (Guid)Marshal.PtrToStructure(activeGuidPtr, typeof(Guid));
                uint   bufferSize       = 1024;
                IntPtr valuePtr         = Marshal.AllocHGlobal((int)bufferSize);
                IntPtr type             = IntPtr.Zero;
                try
                {
                    var success = NativeMethods.PowerReadACValue(IntPtr.Zero, ref activeSchemeGuid, ref SleepCategoryGuid, ref WakeTimersGuid, type, valuePtr, ref bufferSize);

                    if (success == 0)
                    {
                        byte[] bArr = new byte[bufferSize];
                        Marshal.Copy(valuePtr, bArr, 0, (int)bufferSize);
                        wakeTimersAllowed = BitConverter.ToBoolean(bArr, 0);
                    }
                    else
                    {
                        wakeTimersAllowed = false;
                    }
                }
                catch (Exception)
                {
                    var ex = Win32ExceptionManager.GetWin32Exception();
                    throw ex;
                }
                finally
                {
                    Marshal.FreeHGlobal(valuePtr);
                }

                return(wakeTimersAllowed);
            }
            else
            {
                return(true);
            }
        }
Example #5
0
        /// <summary>
        /// Gets the current power policy.
        /// </summary>
        /// <returns>A PowerPolicies struct representing the current power policy.</returns>
        public static PowerPolicies GetCurrentPowerPolicy()
        {
            GLOBAL_POWER_POLICY globalPowerPolicy = new GLOBAL_POWER_POLICY();
            POWER_POLICY        powerPolicy       = new POWER_POLICY();

            if (!NativeMethods.GetCurrentPowerPolicies(out globalPowerPolicy, out powerPolicy))
            {
                var ex = Win32ExceptionManager.GetWin32Exception();
                throw ex;
            }

            PowerPolicies ppRet = new PowerPolicies();

            ppRet.GlobalPowerPolicy = globalPowerPolicy;
            ppRet.PowerPolicy       = powerPolicy;
            return(ppRet);
        }
Example #6
0
        private static FILE_INFO_3[] GetResourcesOpenedInNetworkShareFileInfo3()
        {
            IList <FILE_INFO_3> openedResources = new List <FILE_INFO_3>();
            int    entriesRead;
            int    totalEntries;
            IntPtr buffer = IntPtr.Zero;

            try
            {
                FILE_INFO_3 fileInfo3 = new FILE_INFO_3();
                int         status    = NativeMethods.NetFileEnum(null, null, null, 3, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, IntPtr.Zero);

                if (status != 0)
                {
                    var ex = Win32ExceptionManager.GetWin32Exception();
                    throw ex;
                }

                for (int i = 0; i < entriesRead; i++)
                {
                    IntPtr iPtr = new IntPtr(buffer.ToInt32() + (i * Marshal.SizeOf(fileInfo3)));
                    fileInfo3 = (FILE_INFO_3)Marshal.PtrToStructure(iPtr, typeof(FILE_INFO_3));

                    // Only add files/folder where the permission is at least 'read'.
                    if (fileInfo3.fi3_permission != 0)
                    {
                        openedResources.Add(fileInfo3);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                NativeMethods.NetApiBufferFree(buffer);
            }

            return(openedResources.ToArray <FILE_INFO_3>());
        }
Example #7
0
        /// <summary>
        /// Enable standby using power availability request.
        /// </summary>
        private void EnableStandbyPowerAvailabilityRequest()
        {
            try
            {
                lock (lockObj)
                {
                    bool standbyWasSuspended = this.standbySuspended;

                    if (this.currentPowerRequest != IntPtr.Zero)
                    {
                        var success = NativeMethods.PowerClearRequest(this.currentPowerRequest, PowerRequestType.PowerRequestSystemRequired);

                        if (!success)
                        {
                            this.currentPowerRequest = IntPtr.Zero;
                            var ex = Win32ExceptionManager.GetWin32Exception();
                            throw new WsapmException(Resources.Wsapm_Core.StandbyManager_StandbyEnableFailed, ex);
                        }
                        else
                        {
                            this.currentPowerRequest = IntPtr.Zero;
                            this.standbySuspended    = false;
                        }
                    }

                    if (standbyWasSuspended)
                    {
                        WsapmLog.Log.WriteLine(Resources.Wsapm_Core.StandbyManager_StandbyEnabled, LogMode.Normal); // Only write log if standby was suspended before.
                    }
                }
            }
            catch (Exception ex)
            {
                WsapmLog.Log.WriteError(ex);
            }
        }