It is currently Mon Aug 19, 2019 3:05 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject:
PostPosted: Thu Sep 12, 2002 1:02 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
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:
; -----------------------------------------------------------------------------------------------------
; 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/
--


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Sep 12, 2002 7:30 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Mar 18, 2007 11:46 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4572
Location: Spa, relaxing and thinking, and learning...
Updated to PB 4.02:
Code:
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Mar 18, 2007 11:55 pm 
Offline
Always Here
Always Here
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 5754
Location: Berlin - Germany
A bit shorter, work for PB3.9x and PB4
Code:
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.70 | SpiderBasic 2.21 | Windows 10 Pro (x64) | Linux Mint 19.2 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Mar 19, 2007 9:21 am 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2430
Great example :!: - and so easy for everyone to make a tool around it: http://sudokuprogram.googlepages.com/Shutdown.exe


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Sep 17, 2007 8:49 am 
Offline
New User
New User

Joined: Sun Sep 16, 2007 1:18 pm
Posts: 3
Location: Russia, Moscow
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) ?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Sep 17, 2007 9:18 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Apr 02, 2004 12:21 pm
Posts: 210
Location: germany/thueringen
for xp/vista (2000 ?) ...

Code:
#SE_SHUTDOWN_PRIVILEGE = 19
RtlAdjustPrivilege_(#SE_SHUTDOWN_PRIVILEGE,1,0,@ret.b)
NtShutdownSystem_(1) ;restart // powerOff = 2
End
8)

_________________
["1:0>1"]


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Sep 17, 2007 9:33 am 
Offline
New User
New User

Joined: Sun Sep 16, 2007 1:18 pm
Posts: 3
Location: Russia, Moscow
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Sep 17, 2007 9:41 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Apr 02, 2004 12:21 pm
Posts: 210
Location: germany/thueringen
@YaSoC
use NtShutdownSystem_(2) for poweroff !
but should work in server2003 also .

_________________
["1:0>1"]


Top
 Profile  
Reply with quote  
 Post subject: Shutdown poweroff shut down power off
PostPosted: Mon Sep 17, 2007 9:45 am 
Offline
New User
New User

Joined: Sun Sep 16, 2007 1:18 pm
Posts: 3
Location: Russia, Moscow
Shut Down Computer
Power Off Computer

Code:

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]


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye