Page 1 of 1

Posted: Thu Sep 12, 2002 1:02 am
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/
--

Posted: Thu Sep 12, 2002 7:30 am
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

Posted: Sun Mar 18, 2007 11:46 pm
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

Posted: Sun Mar 18, 2007 11:55 pm
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

Posted: Mon Mar 19, 2007 9:21 am
by Michael Vogel
Great example :!: - and so easy for everyone to make a tool around it: http://sudokuprogram.googlepages.com/Shutdown.exe

Posted: Mon Sep 17, 2007 8:49 am
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) ?

Posted: Mon Sep 17, 2007 9:18 am
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)

Posted: Mon Sep 17, 2007 9:33 am
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.

Posted: Mon Sep 17, 2007 9:41 am
by bingo
@YaSoC
use NtShutdownSystem_(2) for poweroff !
but should work in server2003 also .

Shutdown poweroff shut down power off

Posted: Mon Sep 17, 2007 9:45 am
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]

Re: Shutting down Windows

Posted: Fri Sep 13, 2024 12:19 am
by Randy Walker
What's wrong with a simple single liner?

Code: Select all

RunProgram("shutdown","/s /f /t 000","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Ascii)
/s is for shutdown, opposed to /h for hibernate or /r for reboot.
/f is to force execution even if other apps are still busy.
/t 000 is to say wait zero seconds -- execute immediately!!

This is 2nd time I created this post. What happen to my original post?

Re: Shutting down Windows

Posted: Fri Sep 13, 2024 12:28 am
by jacdelad
Or even simpler:

Code: Select all

RunProgram("shutdown","/s /f /t 000","",#PB_Program_Hide)

Re: Shutting down Windows

Posted: Fri Sep 13, 2024 1:06 am
by Randy Walker
jacdelad wrote: Fri Sep 13, 2024 12:28 am Or even simpler:

Code: Select all

RunProgram("shutdown","/s /f /t 000","",#PB_Program_Hide)
Nice!!! What can you do to improve my fingerprinter?
viewtopic.php?t=85190

Re: Shutting down Windows

Posted: Fri Sep 13, 2024 2:09 am
by jacdelad
Randy Walker wrote: Fri Sep 13, 2024 1:06 am Nice!!! What can you do to improve my fingerprinter?
viewtopic.php?t=85190
Not much, I'm afraid.