Page 1 of 1

Sleep mode

Posted: Mon Feb 16, 2026 1:01 pm
by a_carignan
Hello, I would like to know how to put a Windows PC to sleep.

Re: Sleep mode

Posted: Mon Feb 16, 2026 2:27 pm
by Michael Vogel
Not tested:

Code: Select all

RunProgram("rundll32.exe","powrprof.dll,SetSuspendState 0,1,0",".")
The following shutdown procedure does work, but doesn't support 'sleep':

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

Re: Sleep mode

Posted: Tue Feb 17, 2026 5:31 am
by a_carignan
Merci, le premier code fonctionne, mais mets l'appareil en hibernation et je veux qu'une mise en veille ordinaire. Pour le deuxième code, il ne permet pas de dormir.

Re: Sleep mode

Posted: Tue Feb 17, 2026 9:33 am
by BarryG
a_carignan wrote: Mon Feb 16, 2026 1:01 pmHello, I would like to know how to put a Windows PC to sleep.
Image

Re: Sleep mode

Posted: Tue Feb 17, 2026 3:13 pm
by Jacobus
Perhaps so,

; https://learn.microsoft.com/en-us/windo ... spendstate
; BOOLEAN SetSuspendState(
; [in] BOOLEAN bHibernate,
; [in] BOOLEAN bForce,
; [in] BOOLEAN bWakeupEventsDisabled

; [in] bHibernate = If this parameter is TRUE, the system hibernates. If the parameter is FALSE, the system is suspended.
; [in] bForce = This parameter has no effect.
; [in] bWakeupEventsDisabled = If this parameter is TRUE, the system disables all wake events. If the parameter is FALSE, any system wake events remain enabled.

; If the function succeeds, the return value is nonzero. If the function fails, the Return value is zero.

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)

Re: Sleep mode

Posted: Wed Feb 18, 2026 7:27 am
by a_carignan
Merci, mais dans les deux cas mon ordinateur hiberne.

Re: Sleep mode

Posted: Wed Feb 18, 2026 9:09 am
by breeze4me
According to the answer from AI, it is only possible if you turn off hibernation mode.

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);

Re: Sleep mode

Posted: Wed Feb 18, 2026 9:25 pm
by a_carignan
Oups, j'avais laissé une ligne de code qui mettait en hibernation l'ordinateur avant d'appeler votre procédure. Cette ligne de code a été mise en commentaire et votre procédure fonctionne bien.
Merci pour votre aide.
Oops, I left a line of code that put the computer into hibernation before calling your procedure. This line of code has been commented out, and your procedure now works correctly.
Thank you for your help.