Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sendkeys question????`

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
GB
How ould I use sendkeys to press the ctrl-alt-del combination? I donty seem to be able to do it!

Thanks

elziko
 
Are you programming for NT or Win9x?

I guess the answer depends on what you are trying to accomplish.
VCA.gif

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....
 
I'm using Win2K and for quite some time, and unsuccessfully I've been trying to write a bit of code to shut down and power of my computer. However it works from the start menu.

This means that if I could emulate these key presses:

CTRL-ALT-DEL
S
L
S
ENTER

(off the top of my head cos I'm at work with W95)

then I could have my puter shutdown and power off. I know its moving the goalposts somewhat but I need it done and right now I dont care how I do it.

I dont seem to even get a far as getting CTRL-ALT-DEL to work.

I have also tried CTRL-ESC and cant get that to work.

Are you gonna tell me that sendkeys doesn't work in NT/Win2K???

Thanks

elziko
 
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]
VCA.gif

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....
 
if you really want to go for the keys:
Const VK_H = 72
Const VK_E = 69
Const VK_L = 76
Const VK_O = 79
Const KEYEVENTF_KEYUP = &H2
Const INPUT_MOUSE = 0
Const INPUT_KEYBOARD = 1
Const INPUT_HARDWARE = 2
Private Type MOUSEINPUT
dx As Long
dy As Long
mouseData As Long
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type HARDWAREINPUT
uMsg As Long
wParamL As Integer
wParamH As Integer
End Type
Private Type GENERALINPUT
dwType As Long
xi(0 To 23) As Byte
End Type
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub Form_KeyPress(KeyAscii As Integer)
'Print the key on the form
Me.Print Chr$(KeyAscii);
End Sub
Private Sub Form_Paint()
'KPD-Team 2000
'URL: 'E-Mail: KPDTeam@Allapi.net
'Clear the form
Me.Cls
'call the SendKey-function
SendKey VK_H
SendKey VK_E
SendKey VK_L
SendKey VK_L
SendKey VK_O
End Sub
Private Sub SendKey(bKey As Byte)
Dim GInput(0 To 1) As GENERALINPUT
Dim KInput As KEYBDINPUT
KInput.wVk = bKey 'the key we're going to press
KInput.dwFlags = 0 'press the key
'copy the structure into the input array's buffer.
GInput(0).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(0).xi(0), KInput, Len(KInput)
'do the same as above, but for releasing the key
KInput.wVk = bKey ' the key we're going to realease
KInput.dwFlags = KEYEVENTF_KEYUP ' release the key
GInput(1).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(1).xi(0), KInput, Len(KInput)
'send the input now
Call SendInput(2, GInput(0), Len(GInput(0)))
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top