VB编程:怎样向一个已打开的DOS窗口发送按键?
以下程序,我想利用DOS的runas命令,以用户abcd的身份打开记事本,发送命令是成功了,但是在发送密码时不成功,怎么回事啊?谁能帮我改一下PrivateSubComm...
以下程序,我想利用DOS的runas命令,以用户abcd的身份打开记事本,发送命令是成功了,但是在发送密码时不成功,怎么回事啊?谁能帮我改一下
Private Sub Command1_Click()
Dim myHandle As Long, cmd As String
Dim i As Long
myHandle = FindWindow("ConsoleWindowClass", vbNullString)
'发送命令
cmd = "runas /user:abcd notepad.exe"
For i = 1 To Len(cmd)
PostMessage myHandle, WM_CHAR, Asc(Mid$(cmd, i, 1)), 0&
Next i
PostMessage myHandle, WM_CHAR, VK_RETURN, 0&
Delay_mm 3000 '延迟3秒
'发送密码(密码为88888888)
for i=1 to 8
PostMessage myHandle, WM_KEYDOWN, VK_8, 0&
PostMessage myHandle, WM_KEYUP, VK_8, 0&
next i
End Sub 展开
Private Sub Command1_Click()
Dim myHandle As Long, cmd As String
Dim i As Long
myHandle = FindWindow("ConsoleWindowClass", vbNullString)
'发送命令
cmd = "runas /user:abcd notepad.exe"
For i = 1 To Len(cmd)
PostMessage myHandle, WM_CHAR, Asc(Mid$(cmd, i, 1)), 0&
Next i
PostMessage myHandle, WM_CHAR, VK_RETURN, 0&
Delay_mm 3000 '延迟3秒
'发送密码(密码为88888888)
for i=1 to 8
PostMessage myHandle, WM_KEYDOWN, VK_8, 0&
PostMessage myHandle, WM_KEYUP, VK_8, 0&
next i
End Sub 展开
4个回答
2013-11-16
展开全部
1.新建一个工程,窗口里放一个"按钮",双击按钮,粘贴以下代码
Private Sub Command1_Click()
ReturnValue = Shell("cmd.EXE", 1)
Me.WindowState = 1
myHandle = FindWindow(vbNullString, "C:\WINDOWS\system32\cmd.exe")
myval = SetWindowPos(myHandle, -1, 0, 0, 0, 0, 3)
Call keybd_event(80, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(73, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(78, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(71, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(Asc(" "), 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(49, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(50, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(55, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(110, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(48, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(110, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(48, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(110, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(49, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(Asc(" "), 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(111, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(84, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(13, 0, KEYEVENTF_KEYUP, 0)
2.菜单选择project(工程)\Add module(增加模块),粘贴以下代码,F5 看效果
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
注意:系统在不在c:\的,需要更改"C:\WINDOWS\system32\cmd.exe"
Private Sub Command1_Click()
ReturnValue = Shell("cmd.EXE", 1)
Me.WindowState = 1
myHandle = FindWindow(vbNullString, "C:\WINDOWS\system32\cmd.exe")
myval = SetWindowPos(myHandle, -1, 0, 0, 0, 0, 3)
Call keybd_event(80, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(73, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(78, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(71, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(Asc(" "), 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(49, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(50, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(55, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(110, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(48, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(110, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(48, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(110, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(49, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(Asc(" "), 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(111, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(84, 0, KEYEVENTF_KEYUP, 0)
Call keybd_event(13, 0, KEYEVENTF_KEYUP, 0)
2.菜单选择project(工程)\Add module(增加模块),粘贴以下代码,F5 看效果
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
注意:系统在不在c:\的,需要更改"C:\WINDOWS\system32\cmd.exe"
2013-11-16
展开全部
你用 Shell "cmd /p" 比较简单
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-16
展开全部
发送密码也使用 WM_CHAR
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-16
展开全部
用sendkeys "888888"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询