Posted: Thu Sep 12, 2002 1:02 am
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
--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
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

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/
--