OptionExplicit PrivateConst EWX_LogOff AsLong=0 PrivateConst EWX_SHUTDOWN AsLong=1 PrivateConst EWX_REBOOT AsLong=2 PrivateConst EWX_FORCE AsLong=4 PrivateConst EWX_POWEROFF AsLong=8 'The ExitWindowsEx function either logs off, shuts down, or shuts 'down and restarts the system. Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions AsLong, ByVal dwReserved AsLong) AsLong 'The GetLastError function returns the calling thread's last-error 'code value. The last-error code is maintained on a per-thread basis. 'Multiple threads do not overwrite each other's last-error code. Private Declare Function GetLastError Lib "kernel32" () AsLong PrivateConst mlngWindows95 =0 PrivateConst mlngWindowsNT =1 Public glngWhichWindows32 AsLong 'The GetVersion function returns the operating system in use. Private Declare Function GetVersion Lib "kernel32" () AsLong Private Type LUID UsedPart AsLong IgnoredForNowHigh32BitPart AsLong End Type Private Type LUID_AND_ATTRIBUTES TheLuid As LUID Attributes AsLong End Type Private Type TOKEN_PRIVILEGES PrivilegeCount AsLong TheLuid As LUID Attributes AsLong End Type 'The GetCurrentProcess function returns a pseudohandle for the 'current process. Private Declare Function GetCurrentProcess Lib "kernel32" () AsLong 'The OpenProcessToken function opens the access token associated with 'a process. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle AsLong, ByVal DesiredAccess AsLong, TokenHandle AsLong) AsLong 'The LookupPrivilegeValue function retrieves the locally unique 'identifier (LUID) used on a specified system to locally represent 'the specified privilege name. Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName AsString, ByVal lpName AsString, lpLuid As LUID) AsLong 'The AdjustTokenPrivileges function enables or disables privileges 'in the specified access token. Enabling or disabling privileges 'in an access token requires TOKEN_ADJUST_PRIVILEGES access. Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle AsLong, ByVal DisableAllPrivileges AsLong, NewState As TOKEN_PRIVILEGES, ByVal BufferLength AsLong, _ PreviousState As TOKEN_PRIVILEGES, _ ReturnLength AsLong) AsLong Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode AsLong) PrivateSub AdjustToken() '******************************************************************** '* This procedure sets the proper privileges to allow a log off or a '* shut down to occur under Windows NT. '******************************************************************** Const TOKEN_ADJUST_PRIVILEGES =&H20 Const TOKEN_QUERY =&H8 Const SE_PRIVILEGE_ENABLED =&H2 Dim hdlProcessHandle AsLong Dim hdlTokenHandle AsLong Dim tmpLuid As LUID Dim tkp As TOKEN_PRIVILEGES Dim tkpNewButIgnored As TOKEN_PRIVILEGES Dim lBufferNeeded AsLong 'Set the error code of the last thread to zero using the 'SetLast Error function. Do this so that the GetLastError 'function does not return a value other than zero for no 'apparent reason. SetLastError 0 'Use the GetCurrentProcess function to set the hdlProcessHandle 'variable. hdlProcessHandle = GetCurrentProcess() If GetLastError <>0Then MsgBox"GetCurrentProcess error=="& GetLastError EndIf OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle If GetLastError <>0Then MsgBox"OpenProcessToken error=="& GetLastError EndIf 'Get the LUID for shutdown privilege LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid If GetLastError <>0Then MsgBox"LookupPrivilegeValue error=="& GetLastError EndIf tkp.PrivilegeCount =1 ' One privilege to set tkp.TheLuid = tmpLuid tkp.Attributes = SE_PRIVILEGE_ENABLED 'Enable the shutdown privilege in the access token of this process AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded If GetLastError <>0Then MsgBox"AdjustTokenPrivileges error=="& GetLastError EndIf End Sub PrivateSub cmdLogoff_Click() ExitWindowsEx (EWX_LogOff), &HFFFF MsgBox"ExitWindowsEx's GetLastError "& GetLastError End Sub PrivateSub cmdForceLogoff_Click() ExitWindowsEx (EWX_LogOff Or EWX_FORCE), &HFFFF MsgBox"ExitWindowsEx's GetLastError "& GetLastError End Sub PrivateSub cmdShutdown_Click() If glngWhichWindows32 = mlngWindowsNT Then AdjustToken MsgBox"Post-AdjustToken GetLastError "& GetLastError EndIf ExitWindowsEx (EWX_SHUTDOWN), &HFFFF MsgBox"ExitWindowsEx's GetLastError "& GetLastError End Sub PrivateSub cmdForceShutdown_Click() If glngWhichWindows32 = mlngWindowsNT Then AdjustToken MsgBox"Post-AdjustToken GetLastError "& GetLastError EndIf ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF MsgBox"ExitWindowsEx's GetLastError "& GetLastError End Sub PrivateSub Form_Load() '******************************************************************** '* When the project starts, check the operating system used by '* calling the GetVersion function. '******************************************************************** Dim lngVersion AsLong lngVersion = GetVersion() If ((lngVersion And&H80000000) =0) Then glngWhichWindows32 = mlngWindowsNT MsgBox"Running Windows NT" Else glngWhichWindows32 = mlngWindows95 MsgBox"Running Windows 95" EndIf End Sub