Shutting down Windows

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Hi all,

This isn't much use, but it's something I've been trying to get working for a long time! Works on 2000 (NT OS) and Me (9x OS), so should be OK anywhere in theory. Note that 9x waits for quite a long time if you don't use the 'force' flag, but it'll work eventually :wink:

Code: Select all

; -----------------------------------------------------------------------------------------------------
; Stuff undefined or wrongly defined in PB/NASM...
; -----------------------------------------------------------------------------------------------------

#TOKEN_ADJUST_PRIVILEGES  = $20
#TOKEN_QUERY              = $8
#EWX_POWEROFF             = $8

Structure NEW_LUID_AND_ATTRIBUTES
   pLuid.LUID
   Attributes.l
EndStructure

Structure NEW_TOKEN_PRIVILEGES
  PrivilegeCount.l
  Privileges.NEW_LUID_AND_ATTRIBUTES[#ANYSIZE_ARRAY]
EndStructure

; -----------------------------------------------------------------------------------------------------
; Main shutdown/logoff procedure (you can use it directly with ExitWindowsEx_ () flags if you want)...
; -----------------------------------------------------------------------------------------------------

Procedure.b DitchWindows (flags)
  DefType.OSVERSIONINFO os
  os\dwOSVersionInfoSize = SizeOf (OSVERSIONINFO)
  GetVersionEx_ (os)
  If os\dwPlatformId = #VER_PLATFORM_WIN32_NT
    If OpenProcessToken_ (GetCurrentProcess_ (), #TOKEN_ADJUST_PRIVILEGES | #TOKEN_QUERY, @token)
      If LookupPrivilegeValue_ (#NULL, "SeShutdownPrivilege", tkp.NEW_TOKEN_PRIVILEGES\Privileges[0]\pLuid)
        tkp\PrivilegeCount = 1
        tkp\Privileges[0]\Attributes = #SE_PRIVILEGE_ENABLED
        If AdjustTokenPrivileges_ (token, #FALSE, tkp, #NULL, #NULL, #NULL)
          If ExitWindowsEx_ (flags, 0) = 0
            ProcedureReturn 4
          EndIf
        Else
          ProcedureReturn 3
        EndIf
      Else
        ProcedureReturn 2
      EndIf
    Else
      ProcedureReturn 1
    EndIf
  Else
    If ExitWindowsEx_ (flags, 0) = 0
      ProcedureReturn 4
    EndIf
  EndIf
EndProcedure

; -----------------------------------------------------------------------------------------------------
; Convenience functions (force = 1 to ignore applications that don't want to shut down, 0 otherwise)
; -----------------------------------------------------------------------------------------------------

Procedure.b LogOff (force)
  If force
    force = #EWX_FORCE
  EndIf
  flags = #EWX_LOGOFF | force
  DitchWindows (flags)
EndProcedure

Procedure.b ShutDown (force)
  If force
    force = #EWX_FORCE
  EndIf
  flags = #EWX_SHUTDOWN | force
  DitchWindows (flags)
EndProcedure

Procedure.b Reboot (force)
  If force
    force = #EWX_FORCE
  EndIf
  flags = #EWX_REBOOT | force
  DitchWindows (flags)
EndProcedure

Procedure.b PowerOff (force)
  If force
    force = #EWX_FORCE
  EndIf
  flags = #EWX_POWEROFF | force
  DitchWindows (flags)
EndProcedure

; -----------------------------------------------------------------------------------------------------
 ; D E M O . . .
; -----------------------------------------------------------------------------------------------------

If MessageRequester ("Warning!", "Logoff system?", #MB_OKCANCEL | #MB_ICONWARNING) = 1

  error = LogOff (0)

  Select error
    Case 1
      MessageRequester ("Shutdown error", "Failed to get access token for current process", #MB_ICONWARNING)
    Case 2
      MessageRequester ("Shutdown error", "Failed to get required security privilege", #MB_ICONWARNING)
    Case 3
      MessageRequester ("Shutdown error", "Failed to adjust required security privilege", #MB_ICONWARNING)
    Case 4
      MessageRequester ("Shutdown error", "ExitWindowsEx call failed", #MB_ICONWARNING)
  EndSelect

EndIf



--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

Hello,

This code is nice and better the one I had before.

Good.

Thank you for this.


Francois Weil
14, rue Douer
F64100 Bayonne
User avatar
Psychophanta
Addict
Addict
Posts: 4996
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Post by Psychophanta »

Updated to PB 4.02:

Code: Select all

Structure NEW_LUID_AND_ATTRIBUTES
   pLuid.LUID
   Attributes.l
EndStructure
Structure NEW_TOKEN_PRIVILEGES
  PrivilegeCount.l
  Privileges.NEW_LUID_AND_ATTRIBUTES[#ANYSIZE_ARRAY]
EndStructure
; -----------------------------------------------------------------------------------------------------
; Main shutdown/logoff procedure (you can use it directly with ExitWindowsEx_ () flags if you want)...
; -----------------------------------------------------------------------------------------------------
Procedure.b DitchWindows(flags)
  Protected os.OSVERSIONINFO:os\dwOSVersionInfoSize=SizeOf(OSVERSIONINFO)
  GetVersionEx_(os)
  If os\dwPlatformId=#VER_PLATFORM_WIN32_NT
    If OpenProcessToken_(GetCurrentProcess_(),#TOKEN_ADJUST_PRIVILEGES|#TOKEN_QUERY,@token)
      If LookupPrivilegeValue_(#Null,"SeShutdownPrivilege",tkp.NEW_TOKEN_PRIVILEGES\Privileges[0]\pLuid)
        tkp\PrivilegeCount=1
        tkp\Privileges[0]\Attributes=#SE_PRIVILEGE_ENABLED
        If AdjustTokenPrivileges_(token,#False,tkp,#Null,#Null,#Null)
          If ExitWindowsEx_(flags,0)=0:ProcedureReturn 4:EndIf
        Else:ProcedureReturn 3
        EndIf
      Else:ProcedureReturn 2
      EndIf
    Else:ProcedureReturn 1
    EndIf
  Else
    If ExitWindowsEx_(flags,0)=0
      ProcedureReturn 4
    EndIf
  EndIf
EndProcedure
; -----------------------------------------------------------------------------------------------------
; Convenience functions (force = 1 to ignore applications that don't want to shut down, 0 otherwise)
; -----------------------------------------------------------------------------------------------------
Procedure.b LogOff(force=0)
  If force:force=#EWX_FORCE:EndIf
  DitchWindows(#EWX_LOGOFF|force)
EndProcedure
Procedure.b ShutDown(force=0)
  If force:force=#EWX_FORCE:EndIf
  DitchWindows(#EWX_LOGOFF|#EWX_SHUTDOWN|force)
EndProcedure
Procedure.b Reboot(force=0)
  If force:force=#EWX_FORCE:EndIf
  DitchWindows(#EWX_LOGOFF|#EWX_REBOOT|force)
EndProcedure
Procedure.b PowerOff(force=0)
  If force:force=#EWX_FORCE:EndIf
  DitchWindows(#EWX_LOGOFF|#EWX_POWEROFF|force)
EndProcedure
; ----------------------------------------------------------------------------------------------------- 
; D E M O . . .
; -----------------------------------------------------------------------------------------------------
If MessageRequester("Warning!","Logoff system?",#MB_OKCANCEL|#MB_ICONWARNING)=1
  error=LogOff()
  Select error
  Case 1:MessageRequester("Shutdown error","Failed to get access token for current process", #MB_ICONWARNING)
  Case 2:MessageRequester ("Shutdown error", "Failed to get required security privilege", #MB_ICONWARNING)
  Case 3:MessageRequester ("Shutdown error", "Failed to adjust required security privilege", #MB_ICONWARNING)
  Case 4:MessageRequester ("Shutdown error", "ExitWindowsEx call failed", #MB_ICONWARNING)
  EndSelect
EndIf
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

A bit shorter, work for PB3.9x and PB4

Code: Select all

Procedure EnableShutDown()
  Privileges.TOKEN_PRIVILEGES
  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)
EndProcedure

Procedure Shutdown(Flags.l)
  EnableShutDown()
  ExitWindowsEx_(Flags, 0)
EndProcedure
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Great example :!: - and so easy for everyone to make a tool around it: http://sudokuprogram.googlepages.com/Shutdown.exe
YaSoC
New User
New User
Posts: 3
Joined: Sun Sep 16, 2007 1:18 pm
Location: Russia, Moscow

Post by YaSoC »

First code doesnt work.
Maybe cause of version.


Second and third codes just only LOGIIN' OFF current user.
Is there some way to Shut Down the computer (power off) ?
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

Post by bingo »

for xp/vista (2000 ?) ...

Code: Select all

#SE_SHUTDOWN_PRIVILEGE = 19
RtlAdjustPrivilege_(#SE_SHUTDOWN_PRIVILEGE,1,0,@ret.b)
NtShutdownSystem_(1) ;restart // powerOff = 2
End
8)
["1:0>1"]
YaSoC
New User
New User
Posts: 3
Joined: Sun Sep 16, 2007 1:18 pm
Location: Russia, Moscow

Post by YaSoC »

2bingo:
Thank you. But ur code reboots computer.

I'm workin' under WinXP. But the program i am writing should be used under 2003 also.

---

I've also tried to rewrite the second example, given by Psychophanta.
it can Shut the computer down. But i am working on exclaimation of all the other functions. I need only ShutDown.

Thank you very much.
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

Post by bingo »

@YaSoC
use NtShutdownSystem_(2) for poweroff !
but should work in server2003 also .
["1:0>1"]
YaSoC
New User
New User
Posts: 3
Joined: Sun Sep 16, 2007 1:18 pm
Location: Russia, Moscow

Shutdown poweroff shut down power off

Post by YaSoC »

Shut Down Computer
Power Off Computer

Code: Select all


Structure NEW_LUID_AND_ATTRIBUTES
   pLuid.LUID
   Attributes.l
EndStructure
Structure NEW_TOKEN_PRIVILEGES
  PrivilegeCount.l
  Privileges.NEW_LUID_AND_ATTRIBUTES[#ANYSIZE_ARRAY]
EndStructure
; -----------------------------------------------------------------------------------------------------
; Main shutdown/logoff procedure (you can use it directly with ExitWindowsEx_ () flags if you want)...
; -----------------------------------------------------------------------------------------------------
Procedure.b DitchWindows(flags)
  Protected os.OSVERSIONINFO:os\dwOSVersionInfoSize=SizeOf(OSVERSIONINFO)
  GetVersionEx_(os)
  If os\dwPlatformId=#VER_PLATFORM_WIN32_NT
    If OpenProcessToken_(GetCurrentProcess_(),#TOKEN_ADJUST_PRIVILEGES|#TOKEN_QUERY,@token)
      If LookupPrivilegeValue_(#Null,"SeShutdownPrivilege",tkp.NEW_TOKEN_PRIVILEGES\Privileges[0]\pLuid)
        tkp\PrivilegeCount=1
        tkp\Privileges[0]\Attributes=#SE_PRIVILEGE_ENABLED
        If AdjustTokenPrivileges_(token,#False,tkp,#Null,#Null,#Null)
          If ExitWindowsEx_(flags,0)=0:ProcedureReturn 4:EndIf
        Else:ProcedureReturn 3
        EndIf
      Else:ProcedureReturn 2
      EndIf
    Else:ProcedureReturn 1
    EndIf
  Else
    If ExitWindowsEx_(flags,0)=0
      ProcedureReturn 4
    EndIf
  EndIf
EndProcedure
; -----------------------------------------------------------------------------------------------------
; Convenience functions (force = 1 to ignore applications that don't want to shut down, 0 otherwise)
; -----------------------------------------------------------------------------------------------------

Procedure.b ShutDown(force=1)
  If force:force=#EWX_FORCE:EndIf
  DitchWindows(#EWX_LOGOFF|#EWX_SHUTDOWN|force)
EndProcedure

  error=ShutDown()
  Select error
  Case 1:MessageRequester("Shutdown error","Failed to get access token for current process", #MB_ICONWARNING)
  Case 2:MessageRequester ("Shutdown error", "Failed to get required security privilege", #MB_ICONWARNING)
  Case 3:MessageRequester ("Shutdown error", "Failed to adjust required security privilege", #MB_ICONWARNING)
  Case 4:MessageRequester ("Shutdown error", "ExitWindowsEx call failed", #MB_ICONWARNING)
  EndSelect



This works! =)
I'll try also what u say, bingo.[/code]
Post Reply