• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dapplo / Dapplo.Windows / 24262799127

10 Apr 2026 08:30PM UTC coverage: 32.208% (+0.2%) from 31.968%
24262799127

push

github

web-flow
Merge pull request #55 from dapplo/copilot/add-more-apis-for-system-state

Add Dapplo.Windows.SystemState package for system power state management

607 of 2018 branches covered (30.08%)

Branch coverage included in aggregate %.

41 of 74 new or added lines in 4 files covered. (55.41%)

1 existing line in 1 file now uncovered.

1702 of 5151 relevant lines covered (33.04%)

28.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Dapplo.Windows.SystemState/PowerManagementApi.cs
1
// Copyright (c) Dapplo and contributors. All rights reserved.
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3

4
using System.Runtime.InteropServices;
5
using Dapplo.Windows.SystemState.Enums;
6

7
namespace Dapplo.Windows.SystemState;
8

9
/// <summary>
10
/// Provides access to Windows power management API functions.
11
/// </summary>
12
public static class PowerManagementApi
13
{
14
    private const string PowrprofDll = "powrprof.dll";
15
    private const string User32Dll = "user32.dll";
16

17
    /// <summary>
18
    /// Suspends the system by transitioning it to sleep mode or hibernation.
19
    /// See <a href="https://learn.microsoft.com/en-us/windows/win32/api/powrprof/nf-powrprof-setsuspendstate">SetSuspendState function</a>
20
    /// </summary>
21
    /// <param name="hibernate">
22
    /// If <c>true</c>, the system hibernates. If <c>false</c>, the system is suspended.
23
    /// </param>
24
    /// <param name="forceCritical">
25
    /// If <c>true</c>, the system is suspended or hibernated immediately without sending the WM_POWERBROADCAST message.
26
    /// If <c>false</c>, the function broadcasts a WM_POWERBROADCAST message with the PBT_APMSUSPEND parameter value.
27
    /// Applications that have registered for power notification will have the opportunity to prevent the suspend.
28
    /// </param>
29
    /// <param name="disableWakeEvent">
30
    /// If <c>true</c>, the system disables all wake events. If <c>false</c>, enabled wake events remain enabled.
31
    /// </param>
32
    /// <returns><c>true</c> if the function succeeds, otherwise <c>false</c>.</returns>
33
    [DllImport(PowrprofDll, SetLastError = true)]
34
    [return: MarshalAs(UnmanagedType.Bool)]
35
    public static extern bool SetSuspendState(
36
        [MarshalAs(UnmanagedType.Bool)] bool hibernate,
37
        [MarshalAs(UnmanagedType.Bool)] bool forceCritical,
38
        [MarshalAs(UnmanagedType.Bool)] bool disableWakeEvent);
39

40
    /// <summary>
41
    /// Logs off the interactive user, shuts down the system, or shuts down and restarts the system.
42
    /// See <a href="https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-exitwindowsex">ExitWindowsEx function</a>
43
    /// </summary>
44
    /// <param name="uFlags">The shutdown type. One or more <see cref="ExitWindowsFlags"/> values.</param>
45
    /// <param name="dwReason">
46
    /// The reason for initiating the shutdown. This parameter must be one of the system shutdown reason codes.
47
    /// If this parameter is zero, the SHTDN_REASON_FLAG_PLANNED reason code will not be set, and therefore the default
48
    /// action is to create an "unplanned" shutdown.
49
    /// </param>
50
    /// <returns><c>true</c> if the function succeeds, otherwise <c>false</c>.</returns>
51
    [DllImport(User32Dll, SetLastError = true)]
52
    [return: MarshalAs(UnmanagedType.Bool)]
53
    public static extern bool ExitWindowsEx(ExitWindowsFlags uFlags, uint dwReason = 0);
54

55
    /// <summary>
56
    /// Locks the workstation's display.
57
    /// Locking a workstation protects it from unauthorized use.
58
    /// See <a href="https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-lockworkstation">LockWorkStation function</a>
59
    /// </summary>
60
    /// <returns>
61
    /// Because the function executes asynchronously, a return value of <c>true</c> indicates that the operation
62
    /// has been initiated. If <c>false</c>, call GetLastError.
63
    /// </returns>
64
    [DllImport(User32Dll, SetLastError = true)]
65
    [return: MarshalAs(UnmanagedType.Bool)]
66
    public static extern bool LockWorkStation();
67

68
    /// <summary>
69
    /// Suspends the system (puts it to sleep).
70
    /// </summary>
71
    /// <param name="disableWakeEvent">If <c>true</c>, disables all wake events.</param>
72
    /// <returns><c>true</c> if the system was suspended successfully.</returns>
73
    public static bool Sleep(bool disableWakeEvent = false) =>
NEW
74
        SetSuspendState(hibernate: false, forceCritical: false, disableWakeEvent: disableWakeEvent);
×
75

76
    /// <summary>
77
    /// Hibernates the system.
78
    /// </summary>
79
    /// <param name="disableWakeEvent">If <c>true</c>, disables all wake events.</param>
80
    /// <returns><c>true</c> if the system was hibernated successfully.</returns>
81
    public static bool Hibernate(bool disableWakeEvent = false) =>
NEW
82
        SetSuspendState(hibernate: true, forceCritical: false, disableWakeEvent: disableWakeEvent);
×
83

84
    /// <summary>
85
    /// Shuts down the system.
86
    /// The calling process must have the SE_SHUTDOWN_NAME privilege.
87
    /// </summary>
88
    /// <param name="force">If <c>true</c>, forces running applications to close.</param>
89
    /// <returns><c>true</c> if the operation was initiated successfully.</returns>
90
    public static bool Shutdown(bool force = false)
91
    {
NEW
92
        var flags = ExitWindowsFlags.EWX_SHUTDOWN;
×
NEW
93
        if (force)
×
94
        {
NEW
95
            flags |= ExitWindowsFlags.EWX_FORCE;
×
96
        }
NEW
97
        return ExitWindowsEx(flags);
×
98
    }
99

100
    /// <summary>
101
    /// Restarts the system.
102
    /// The calling process must have the SE_SHUTDOWN_NAME privilege.
103
    /// </summary>
104
    /// <param name="force">If <c>true</c>, forces running applications to close.</param>
105
    /// <returns><c>true</c> if the operation was initiated successfully.</returns>
106
    public static bool Restart(bool force = false)
107
    {
NEW
108
        var flags = ExitWindowsFlags.EWX_REBOOT;
×
NEW
109
        if (force)
×
110
        {
NEW
111
            flags |= ExitWindowsFlags.EWX_FORCE;
×
112
        }
NEW
113
        return ExitWindowsEx(flags);
×
114
    }
115

116
    /// <summary>
117
    /// Logs off the current user.
118
    /// </summary>
119
    /// <param name="force">If <c>true</c>, forces running applications to close.</param>
120
    /// <returns><c>true</c> if the operation was initiated successfully.</returns>
121
    public static bool LogOff(bool force = false)
122
    {
NEW
123
        var flags = ExitWindowsFlags.EWX_LOGOFF;
×
NEW
124
        if (force)
×
125
        {
NEW
126
            flags |= ExitWindowsFlags.EWX_FORCE;
×
127
        }
NEW
128
        return ExitWindowsEx(flags);
×
129
    }
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc