Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example:
#COMPILE EXE
#INCLUDE "WIN32API.INC"
SUB ShutDownNT(BYVAL DoWhat AS LONG)
LOCAL OSVersion AS OSVERSIONINFO
LOCAL ProcessHandle AS LONG
LOCAL TokenHandle AS LONG
LOCAL TempLUID AS LUID
LOCAL TokenPrivilleges AS TOKEN_PRIVILEGES
LOCAL TKPDummy AS TOKEN_PRIVILEGES
LOCAL lReturnLength AS LONG
OSVersion.dwOSVersionInfoSize = SIZEOF(OSVersion)
IF GetVersionEx(OSVersion) <> 0 THEN
IF OSVersion.dwPlatformId = %VER_PLATFORM_WIN32_NT THEN
ProcessHandle = GetCurrentProcess()
CALL OpenProcessToken(ProcessHandle, %TOKEN_ADJUST_PRIVILEGES OR %TOKEN_QUERY, TokenHandle)
CALL LookupPrivilegeValue("", "SeShutdownPrivilege", TempLUID)
TokenPrivilleges.PrivilegeCount = 1
TokenPrivilleges.Privileges(0).pLuid = TempLUID
TokenPrivilleges.Privileges(0).Attributes = %SE_PRIVILEGE_ENABLED
IF AdjustTokenPrivileges(TokenHandle, %FALSE, TokenPrivilleges, LEN(TKPDummy), TKPDummy, lReturnLength) THEN
' Flags: %EWX_LOGOFF, %EWX_SHUTDOWN, %EWX_REBOOT, %EWX_FORCE, %EWX_POWEROFF
SELECT CASE DoWhat
CASE 1
' Force Shut Down and ReBoot
CALL ExitWindowsEx(%EWX_FORCE OR %EWX_SHUTDOWN OR %EWX_REBOOT, 0)
CASE 2
' Force Logoff user' Force Logoff user
CALL ExitWindowsEx(%EWX_FORCE OR %EWX_LOGOFF, 0)
CASE ELSE
' Force Shut Down and Power Off
CALL ExitWindowsEx(%EWX_FORCE OR %EWX_SHUTDOWN OR %EWX_POWEROFF, 0)
END SELECT
END IF
END IF
END IF
END SUB
FUNCTION PBMAIN() AS LONG
CALL ShutDownNT(1)
END FUNCTION
http://www.garybeene.com/sw/gbsnippets.htm