Repository navigation
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
107 lines (87 loc) · 3.31 KB
/
Copy pathNativeMethods.cs
File metadata and controls
107 lines (87 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Runtime.InteropServices;
namespace ScreenSleep;
internal static class NativeMethods
{
private static readonly IntPtr HwndBroadcast = new(0xFFFF);
private const uint WmSysCommand = 0x0112;
private static readonly IntPtr ScMonitorPower = new(0xF170);
private const uint SmtoAbortIfHung = 0x0002;
private const uint EsContinuous = 0x80000000;
private const uint EsSystemRequired = 0x00000001;
private const uint EsDisplayRequired = 0x00000002;
internal delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
internal struct NativePoint
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MouseHookData
{
public NativePoint Point;
public uint MouseData;
public uint Flags;
public uint Time;
public UIntPtr ExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct PowerBroadcastSetting
{
public Guid PowerSetting;
public uint DataLength;
}
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(int idHook, HookProc callback, IntPtr module, uint threadId);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll")]
internal static extern IntPtr CallNextHookEx(IntPtr hook, int code, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr GetModuleHandle(string? moduleName);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SendMessageTimeout(
IntPtr window,
uint message,
IntPtr wParam,
IntPtr lParam,
uint flags,
uint timeout,
out IntPtr result);
[DllImport("kernel32.dll")]
private static extern uint SetThreadExecutionState(uint executionState);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr RegisterPowerSettingNotification(
IntPtr recipient,
ref Guid powerSettingGuid,
uint flags);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool UnregisterPowerSettingNotification(IntPtr handle);
internal static void TurnOffDisplay()
=> SendMonitorPowerCommand(new IntPtr(2));
internal static void WakeDisplay()
{
SetThreadExecutionState(EsContinuous | EsSystemRequired | EsDisplayRequired);
SendMonitorPowerCommand(new IntPtr(-1));
SetThreadExecutionState(EsContinuous);
}
private static void SendMonitorPowerCommand(IntPtr state)
{
SendMessageTimeout(
HwndBroadcast,
WmSysCommand,
ScMonitorPower,
state,
SmtoAbortIfHung,
1000,
out _);
}
internal static void KeepSystemAwake()
=> SetThreadExecutionState(EsContinuous | EsSystemRequired);
internal static void AllowSystemSleep()
=> SetThreadExecutionState(EsContinuous);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool DestroyIcon(IntPtr icon);
}