CONSTANT INTEGER EWX_REBOOT = 2
CONSTANT INTEGER EWX_FORCE = 4
// author Philip Salgannik
LUID tLUID
ULong hProcess
Long hToken
TOKEN_PRIVILEGES tTPOld, tTP
Long lTpOld, lR, ll_size
string ls_null
boolean NTEnableShutDown
SetNull(ls_null)
lR = LookupPrivilegevalue(ls_null, SE_SHUTDOWN_NAME, tLUID)
IF (lR <> 0) THEN
// Get the current process handle:
hProcess = GetCurrentProcess()
IF (hProcess <> 0) THEN
lR = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken)
IF (lR <> 0) THEN
//Ok we can now adjust the shutdown priviledges:
tTP.PrivilegeCount = 1
tTP.Privileges.Attributes = SE_PRIVILEGE_ENABLED
tTP.Privileges.pLuid.HighPart = tLUID.HighPart
tTP.Privileges.pLuid.LowPart = tLUID.LowPart
//Now allow this process to shutdown the system:
ll_size = 16 //sizeof(tTP)
lR = AdjustTokenPrivileges(hToken, 0, tTP, ll_size, tTPOld, lTpOld)
IF (lR <> 0) THEN
NTEnableShutDown = True
ELSE
MessageBox("Error", "Can't enable shutdown:You do not have the privileges to shutdown this system.")
END IF
CloseHandle(hToken)
END IF
END IF
END IF
IF NOT NTEnableShutDown THEN RETURN
lR = ExitWindowsEx(ewx_shutdown, 0)
IF (lR = 0) THEN
MessageBox("Error", "ShutdownSystem failed")
RETURN
ELSE
RETURN
END IF