Sleep mode

Windows specific forum
User avatar
a_carignan
Enthusiast
Enthusiast
Posts: 102
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Sleep mode

Post by a_carignan »

Hello, I would like to know how to put a Windows PC to sleep.
User avatar
Michael Vogel
Addict
Addict
Posts: 2860
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Sleep mode

Post 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
User avatar
a_carignan
Enthusiast
Enthusiast
Posts: 102
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Sleep mode

Post 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.
BarryG
Addict
Addict
Posts: 4333
Joined: Thu Apr 18, 2019 8:17 am

Re: Sleep mode

Post 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
User avatar
Jacobus
Enthusiast
Enthusiast
Posts: 166
Joined: Wed Nov 16, 2005 7:51 pm
Location: France
Contact:

Re: Sleep mode

Post 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)
PureBasicien tu es, PureBasicien tu resteras.
User avatar
a_carignan
Enthusiast
Enthusiast
Posts: 102
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Sleep mode

Post by a_carignan »

Merci, mais dans les deux cas mon ordinateur hiberne.
breeze4me
Enthusiast
Enthusiast
Posts: 672
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Sleep mode

Post 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);
User avatar
a_carignan
Enthusiast
Enthusiast
Posts: 102
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Sleep mode

Post 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.
Post Reply