Results 1 to 13 of 13

Thread: Send Commands To CMD Window (without sendkeys)?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Exclamation 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?

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Send Commands To CMD Window (without sendkeys)?

    Make a bat file and shell it instead.

    Code:
    Shell "d:\test.bat", vbHide

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Send Commands To CMD Window (without sendkeys)?

    Quote Originally Posted by Eduardo- View Post
    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.

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,392

    Re: Send Commands To CMD Window (without sendkeys)?


  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Send Commands To CMD Window (without sendkeys)?

    That worked great! Just can't get it to perform the carriage return. It just sits there.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    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.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Send Commands To CMD Window (without sendkeys)?

    Quote Originally Posted by DataMiser View Post
    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.

  9. #9
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: Send Commands To CMD Window (without sendkeys)?

    Try chr(10) or vblf instead vbcrlf.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Send Commands To CMD Window (without sendkeys)?

    Quote Originally Posted by georgekar View Post
    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?

  11. #11
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: Send Commands To CMD Window (without sendkeys)?

    Last edited by georgekar; Sep 24th, 2020 at 11:24 AM.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Send Commands To CMD Window (without sendkeys)?

    Quote Originally Posted by georgekar View Post
    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.

  13. #13
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    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
  •  



Click Here to Expand Forum to Full Width