Skip to content

Instantly share code, notes, and snippets.

@mmmunk
Last active March 30, 2021 08:56
Show Gist options
  • Save mmmunk/545425e68f3d7f2d434d0d51eaf30e3a to your computer and use it in GitHub Desktop.
Save mmmunk/545425e68f3d7f2d434d0d51eaf30e3a to your computer and use it in GitHub Desktop.
Keep Windows awake
// Build: x86_64-w64-mingw32-gcc -Wall -mwindows -Os -s -o Keep-Windows-Awake.exe Keep-Windows-Awake.c
#include <windows.h>
int main() {
/* Keep Windows awake for the lifetime of this thread */
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
/* Wait via a messagebox */
MessageBox(NULL, "Windows is now being prevented from falling asleep.\n\nPress OK to end this...", "Keep-Windows-Awake", MB_OK | MB_ICONINFORMATION);
}
unit UKeepAwake;
interface
uses
Windows;
type
EXECUTION_STATE = DWORD;
const
ES_SYSTEM_REQUIRED = $00000001;
ES_DISPLAY_REQUIRED = $00000002;
ES_USER_PRESENT = $00000004;
ES_AWAYMODE_REQUIRED = $00000040;
ES_CONTINUOUS = $80000000;
function SetThreadExecutionState(esFlags: EXECUTION_STATE): EXECUTION_STATE; stdcall; external kernel32;
procedure KeepAwake;
implementation
procedure KeepAwake;
begin
SetThreadExecutionState(ES_SYSTEM_REQUIRED or ES_DISPLAY_REQUIRED);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment