Windows API

BlitzMax Forums/BlitzMax Beginners Area/Windows API

Good work Yan. Works on my system.

I've created a set of functions and a GUI test window to try them out.

Functions:

Standby [flag]
Hibernate [flag]
Shutdown [flag]
Restart [flag]
LogOff [flag]

If optional flag is TRUE all programs are forced to close



GUI Test Window
SuperStrict

Framework BRL.Win32MaxGui

Import "Shutdown.bmx"

Global win:TGadget=CreateWindow("ShutDown Test",200,40,206,122,Null,WINDOW_TITLEBAR)
Global combo:TGadget=CreateComboBox(20,32,80,20,win)
AddGadgetItem combo,"Standby"
AddGadgetItem combo,"Log Off"
AddGadgetItem combo,"Hibernate"
AddGadgetItem combo,"Shutdown"
AddGadgetItem combo,"Restart"
SelectGadgetItem combo,0
Global btn:TGadget=CreateButton("Do it",128,32,50,22,win,1)
Global forcebtn:TGadget=CreateButton("Force other programs to close",20,68,180,24,win,BUTTON_CHECKBOX)

'-mainloop--------------------------------------------------------------

Repeat
	Select WaitEvent()
		Case EVENT_GADGETACTION
		If EventSource()=btn
			Select SelectedGadgetItem(combo)
				Case 0 ; Standby ButtonState(forcebtn)
				Case 1 ; LogOff ButtonState(forcebtn)
				Case 2 ; Hibernate ButtonState(forcebtn)
				Case 3 ; Shutdown ButtonState(forcebtn)
				Case 4 ; Restart ButtonState(forcebtn)
			End Select
		EndIf
		Case EVENT_WINDOWCLOSE
		Exit
	End Select
Forever


Shutdown Functions - Shutdown.bmx
Rem
	ShutDown functions
   ____________________________
	Standby [flag]
	Hibernate [flag]
	Shutdown [flag]
	Restart [flag]
	LogOff [flag]
	
	If optional flag is TRUE all programs are forced to close
EndRem


Strict

Import brl.retro
Import "-lkernel32"
Import "-ladvapi32"

Extern "Win32"
	'advapi32
	Function OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle:Byte Ptr)
	Function LookupPrivilegeValue(lpSystemName$z, lpName$z, lpLuid:Byte Ptr) = "LookupPrivilegeValueA@12"
	Function AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState:Byte Ptr,..
		BufferLength, PreviousState:Byte Ptr, ReturnLength:Byte Ptr)
	'kernel32
	Function GetVersion()
	Function GetCurrentProcess()
	Function SetSystemPowerState(fSuspend, fForce)
	Function ExitWindowsEx(uFlags%,dwReserved%=0)
	Function GetLastError()                               
End Extern

Function Standby(forced%=False)
	If EnableShutdownPrivileges()
		SetSystemPowerState(True, forced)
	EndIf
End Function

Function Hibernate(forced%=False)
	If EnableShutdownPrivileges()
		SetSystemPowerState(False, forced)
	EndIf	
End Function

Function Shutdown(forced%=False)
	If EnableShutdownPrivileges()
		ExitWindowsEx $8 | $4 * forced
	EndIf
End Function

Function Restart(forced%=False)
	If EnableShutdownPrivileges()
		ExitWindowsEx $2 | $4 * forced
	EndIf
End Function

Function LogOff(forced%=False)
	If EnableShutdownPrivileges()
		ExitWindowsEx $0 | $4 * forced
	EndIf
End Function

Function EnableShutdownPrivileges%()
	Const TOKEN_ADJUST_PRIVILEGES% = $20
	Const TOKEN_QUERY% = $8
	Const SE_PRIVILEGE_ENABLED% = $2
	Local tokenPrivilege%[4]
	If LookupPrivilegeValue(Null, "SeShutdownPrivilege", Byte Ptr(tokenPrivilege)+4)
		Local hProc% = GetCurrentProcess() ' -1
		Local hToken%
		If OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, Varptr(hToken))
			tokenPrivilege[0] = 1
			tokenPrivilege[3] = SE_PRIVILEGE_ENABLED
			If AdjustTokenPrivileges(hToken, False, Byte Ptr(tokenPrivilege), Null, Null, Null)
				Return True
			EndIf
		EndIf
	EndIf
End Function