Sleep mode
Posted: Mon Feb 16, 2026 1:01 pm
Hello, I would like to know how to put a Windows PC to sleep.
Code: Select all
RunProgram("rundll32.exe","powrprof.dll,SetSuspendState 0,1,0",".")Code: Select all
Procedure ShutDown(flags)
; Flags:
; EWX_LOGOFF = 0
; EWX_SHUTDOWN = 1
; EWX_REBOOT = 2
; EWX_FORCE = 4
; EWX_POWEROFF = 8
Protected Privileges.TOKEN_PRIVILEGES
Protected hToken
OpenProcessToken_(GetCurrentProcess_(),40,@hToken)
Privileges\PrivilegeCount=1
Privileges\Privileges[0]\Attributes=#SE_PRIVILEGE_ENABLED
LookupPrivilegeValue_(0,"SeShutdownPrivilege",@Privileges\Privileges[0]\Luid)
AdjustTokenPrivileges_(hToken,0,@Privileges,0,0,0)
CloseHandle_(hToken)
ExitWindowsEx_(flags, 0)
EndProcedure
a_carignan wrote: Mon Feb 16, 2026 1:01 pmHello, I would like to know how to put a Windows PC to sleep.

Code: Select all
; - Test:
Procedure StandbyPC(SuspendState.l)
If OpenLibrary(0,"powrprof.dll") And GetFunction(0,"SetSuspendState")
If SuspendState = 0
CallFunction(0,"SetSuspendState",#False,#False,#False) ; For standby mode
ElseIf SuspendState = 1
CallFunction(0,"SetSuspendState",#True,#False,#False) ; For hibernation
EndIf
CloseLibrary(0)
EndIf
EndProcedure
StandbyPC(0)
Code: Select all
To force a Windows PC into normal sleep mode (S3) rather than hibernation (S4) using Win32 methods,
you must ensure hibernation is disabled,
as the Win32 SetSuspendState function often defaults to hibernation if it is enabled.
Here are the most effective methods to achieve this:
Method 1: Disable Hibernation (Required)
If hibernation is enabled, the command to sleep will often trigger hibernation instead.
1. Open Command Prompt as Administrator.
2. Type the following command to disable hibernation:
powercfg.exe /hibernate off
3. Press Enter.
Once this is disabled, any Win32 call to sleep will function as normal sleep mode.
Method 2: Win32 API Call (C++/C#)
Use the SetSuspendState function from powrprof.h.
#include <windows.h>
#include <powrprof.h>
#pragma comment(lib, "Powrprof.lib")
// Hibernate: FALSE (Sleep)
// ForceCritical: FALSE
// DisableWakeEvent: FALSE
SetSuspendState(FALSE, FALSE, FALSE);