I don't think you want to use SendKeys for this task.
I don't have a Win2k system to try this on but it works fine on both NT and Win9x. Since the documentation on the ExitWindows function at MSDN/Microsoft seems to lump Win2k together with NT, I am assuming you might be able to control shutdown/reboot/logoff using the same methods. I could easily be wrong but I don't see how it could hurt to try the following code. Microsoft probably has a distinct value to identify Win2k (different from VER_PLATFORM_WIN32_NT = 2) but I couldn't find one. You might try skipping the IsWinNT function and running the EnableShutDown sub by default, just to see if it works under Win2k.
Module level...
[tt]
Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
[/tt]'I couldn't determine the values of the following two constants[tt]
'Public Const EWX_POWEROFF
'Public Const EWX_FORCEIFHUNG
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const ANYSIZE_ARRAY = 1
Public Const VER_PLATFORM_WIN32_NT = 2
Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Public Type LUID
LowPart As Long
HighPart As Long
End Type
Public Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type
Private Declare Function GetCurrentProcess Lib _
"kernel32.dll" () As Long
Private Declare Function OpenProcessToken Lib _
"advapi32.dll" (ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib _
"advapi32.dll" Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, ByVal lpName As String, _
lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib _
"advapi32.dll" (ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
PreviousState As TOKEN_PRIVILEGES, _
ReturnLength As Long) As Long
Private Declare Function ExitWindowsEx Lib _
"user32.dll" (ByVal uFlags As Long, _
ByVal dwReserved As Long) As Long
Private Declare Function GetVersionEx Lib _
"kernel32.dll" Alias "GetVersionExA" _
(ByRef lpVersionInformation As OSVERSIONINFO) As Long
Public Function IsWinNT() As Boolean
Dim myOS As OSVERSIONINFO
myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function
Private Sub EnableShutDown()
Dim hProc As Long
Dim hToken As Long
Dim mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES
Dim mNewPriv As TOKEN_PRIVILEGES
hProc = GetCurrentProcess()
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
mPriv.Privileges(0).pLuid = mLUID
' enable shutdown privilege for the current application
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)
End Sub[/tt]
Form level....
[tt]
Private Sub Form_Load()
Command1.Caption = "Shut Down"
Command2.Caption = "Reboot"
Command3.Caption = "Log Off"
End Sub
Private Sub Command1_Click()
'SHUTDOWN THE COMPUTER
Dim ret As Long
Dim Flags As Long
Flags = EWX_SHUTDOWN
Force = True '[/tt]for example[tt]
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub
Private Sub Command2_Click()
'REBOOT THE COMPUTER
Dim ret As Long
Dim Flags As Long
Flags = EWX_REBOOT
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub
Private Sub Command3_Click()
'LOGOFF CURRENT USER
Dim ret As Long
Dim Flags As Long
Flags = EWX_LOGOFF
If Force Then Flags = Flags + EWX_FORCE
ExitWindowsEx Flags, 0
End Sub
[/tt]
Alt255@Vorpalcom.Intranets.com
"If you can get people to ask the wrong questions, they'll never find the right answers."[tt]
Thomas Pynchon[/tt]
Perhaps the reverse is also true....