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
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
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.
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.
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]