-
Sep 23rd, 2020, 03:58 PM
#1
Thread Starter
Lively Member
Send Commands To CMD Window (without sendkeys)?
Is there any way to send keystrokes to a CMD window without using SendKeys or even giving the CMD window focus?
-
Sep 23rd, 2020, 04:14 PM
#2
Re: Send Commands To CMD Window (without sendkeys)?
Make a bat file and shell it instead.
Code:
Shell "d:\test.bat", vbHide
-
Sep 23rd, 2020, 04:25 PM
#3
Thread Starter
Lively Member
Re: Send Commands To CMD Window (without sendkeys)?
Originally Posted by Eduardo-
Make a bat file and shell it instead.
Code:
Shell "d:\test.bat", vbHide
There's already a .bat file open and the console running. I want to be able to throw commands at it from my VB6 program.
-
Sep 23rd, 2020, 04:32 PM
#4
Re: Send Commands To CMD Window (without sendkeys)?
-
Sep 23rd, 2020, 05:22 PM
#5
Thread Starter
Lively Member
Re: Send Commands To CMD Window (without sendkeys)?
Originally Posted by jdc2000
That worked great! Just can't get it to perform the carriage return. It just sits there.
-
Sep 23rd, 2020, 05:26 PM
#6
Thread Starter
Lively Member
Re: Send Commands To CMD Window (without sendkeys)?
Here is what I'm using
Code:
Option Explicit
Private Const WM_CHAR = &H102
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Sub Form_Load()
'open a hidden dos prompt window (WinXP).
Shell "c:\windows\system32\cmd.exe"
End Sub
Private Sub Command1_Click()
Dim h As Long
Dim result As Boolean
' find dos prompt window
h = FindWindow(vbNullString, "c:\windows\system32\cmd.exe")
If h Then
' send "calc.exe" followed by carraige return
result = SendTxt(h, "calc.exe" & vbCr)
result = SendTxt(h, "echo tteesstt" & vbCr)
' optional, check postmessage result
If result = False Then MsgBox "postmessage failed!"
'close the hidden dos prompt window
'SendTxt h, "exit" & vbCr
Else
MsgBox "dos prompt window not found"
End If
End Sub
Private Function SendTxt(ByVal Handle As Long, ByVal sText As String) As Boolean
Dim i As Integer
Dim lngReturn As Long
Dim i1 As Long, last1 As Long
For i = 1 To Len(sText)
i1 = Asc(Mid$(sText, i, 1))
If i1 = last1 Then Call PostMessage(Handle, WM_CHAR, 0, 0&)
lngReturn = PostMessage(Handle, WM_CHAR, i1, 0&)
If lngReturn <> 1 Then Exit Function ' failed
last1 = i1
Next i
SendTxt = True ' passed
End Function
It sends keystrokes to the CMD window, but doesn't press Enter.
-
Sep 23rd, 2020, 06:31 PM
#7
Re: Send Commands To CMD Window (without sendkeys)?
So you are trying to open a command window so you can execute calc.exe? That really makes no sense. Why not just open calc.exe using one of the shell functions and bypass the command window compeltely?
-
Sep 23rd, 2020, 06:44 PM
#8
Thread Starter
Lively Member
Re: Send Commands To CMD Window (without sendkeys)?
Originally Posted by DataMiser
So you are trying to open a command window so you can execute calc.exe? That really makes no sense. Why not just open calc.exe using one of the shell functions and bypass the command window compeltely?
My apologies... that was the example code from the link jdc2000 provided. I have since changed the "calc.exe" line to be whatever I wanted sent to the running batch file window. Now, why am I continuing this thread, you may ask? Because, as I stated directly above your comment, the batch file is not receiving the proposed "vbCr". I've also tried vbcrlf, chr(10), chr(13), chr$(10), chr$(13), and VBNewLine. Nothing I throw at this thing is implementing the carriage return.
If you want to know exactly what I'm running, it's a Rust server, which is a .bat file. Not sure why it's not receiving any of the carriage returns I throw at it, although it does receive the text. When I open the usual cmd, it receives all text and carriage returns just fine... but this thing is apparently a different beast, though still a .bat file, so you'd think all carriage returns would be the same.
And yet... the problem remains.
-
Sep 24th, 2020, 01:34 AM
#9
Re: Send Commands To CMD Window (without sendkeys)?
Try chr(10) or vblf instead vbcrlf.
-
Sep 24th, 2020, 07:47 AM
#10
Thread Starter
Lively Member
Re: Send Commands To CMD Window (without sendkeys)?
Originally Posted by georgekar
Try chr(10) or vblf instead vbcrlf.
As stated above (#8), I have tried Chr(10). I just now attempted vblf, but it did not work. For some reason, when I use VbCrlf or VbLf or Chr(10), it just keeps putting the text in the console window over and over... never actually performing a carriage return.
Is there a chance that something inside the .bat file is preventing a programmatic carriage return?
-
Sep 24th, 2020, 10:23 AM
#11
Re: Send Commands To CMD Window (without sendkeys)?
Last edited by georgekar; Sep 24th, 2020 at 11:24 AM.
-
Sep 25th, 2020, 09:38 AM
#12
Thread Starter
Lively Member
Re: Send Commands To CMD Window (without sendkeys)?
Originally Posted by georgekar
Can you explain how that would work with what I'm doing? I mean, that looks pretty complicated, as I am only wanting to send text to a separate cmd console window.
-
Sep 25th, 2020, 12:50 PM
#13
Re: Send Commands To CMD Window (without sendkeys)?
Get this file: https://github.com/M2000Interpreter/.../ShellPipe.cls
And this file: https://github.com/M2000Interpreter/...r/SPBuffer.cls
These are the events ( Dim withevents M as ShellPipe) https://docs.microsoft.com/en-us/dot...ers/withevents
Public Event DataArrival(ByVal CharsTotal As Long)
Public Event ErrDataArrival(ByVal CharsTotal As Long)
Public Event EOF(ByVal EOFType As SPEOF_TYPES)
Public Event ErrEOF(ByVal EOFType As SPEOF_TYPES)
Public Event Error(ByVal Number As Long, _
ByVal Source As String, _
CancelDisplay As Boolean)
Public Event ChildFinished()
Forget Events, you can use it without them (just make a new shellpipe)
Public Properties:
Active (true if the exe is running, false if stopped)
Public Function Run( _
ByVal CommandLine As String, _
Optional ByVal CurrentDir As String = vbNullString) _
As SP_RESULTS
Public Sub SendLine(ByVal Line As String, Optional ByVal UseLFs As Boolean = True)
You have to use once the Run method then you have to make a loop (and a wait function or place the code inside a timer in a form) and call the ProcessLoop until not active
make a text box and send a line usiing SendLine method.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|