Results 1 to 13 of 13

Thread: sending text to cmd.exe?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    sending text to cmd.exe?

    hello, how to send a text to a command prompt?

    thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sending text to cmd.exe?

    You can pass parameters to CMD.exe when you ShellExecute it.

    This example will pass the DIR command, enter C:\, and output the directory listing to the file C:\Dir.txt
    The /K switch is to keep the DOS window open but you can change it to the /C to close it. Also, SW_HIDE to make
    it totally transparent to the user.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_HIDE As Long = 0
    7. Private Const SW_SHOWNORMAL As Long = 1
    8.  
    9. Private Sub Command1_Click()
    10.     ShellExecute Me.hwnd, "Open", "C:\Windows\System32\CMD.exe", " /K Dir C:\  > C:\Dir.txt", "C:\", SW_SHOWNORMAL
    11. End Sub
    Note: you would want to use the GetSystemDirectory API to dynamically get the directory where the CMD.EXE program is located.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286

    Re: sending text to cmd.exe?

    If the window is already open, and you want to send text to it, treat it like any other standard window. I believe the classname is "tty" so pass that through FindWindow and then use PostMessage to send WM_KEYDOWN and WM_KEYUP (or use keybd_event())

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sending text to cmd.exe?

    Megatron, it was my understanding that the Console window's messagestream was unavailable for sending messages to?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: sending text to cmd.exe?

    I think the CreatePipe can do something like this :

    VB Code:
    1. 'Redirects output from console program to textbox.
    2. 'Requires two textboxes and one command button.
    3. 'Set MultiLine property of Text2 to true.
    4. '
    5. 'Original bcx version of this program was made by
    6. 'VB port was made by Jernej Simoncic <[email protected]>
    7. 'Visit Jernejs site at [url]http://www2.arnes.si/~sopjsimo/[/url]
    8. '
    9. 'Note: don't run plain DOS programs with this example
    10. 'under Windows 95,98 and ME, as the program freezes when
    11. 'execution of program is finnished.
    12.  
    13. Option Explicit
    14. Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
    15. Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
    16. Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    17. Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    18. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
    19. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    20. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    21.  
    22. Private Type SECURITY_ATTRIBUTES
    23.   nLength As Long
    24.   lpSecurityDescriptor As Long
    25.   bInheritHandle As Long
    26. End Type
    27.  
    28. Private Type PROCESS_INFORMATION
    29.   hProcess As Long
    30.   hThread As Long
    31.   dwProcessId As Long
    32.   dwThreadId As Long
    33. End Type
    34.  
    35. Private Type STARTUPINFO
    36.   cb As Long
    37.   lpReserved As Long
    38.   lpDesktop As Long
    39.   lpTitle As Long
    40.   dwX As Long
    41.   dwY As Long
    42.   dwXSize As Long
    43.   dwYSize As Long
    44.   dwXCountChars As Long
    45.   dwYCountChars As Long
    46.   dwFillAttribute As Long
    47.   dwFlags As Long
    48.   wShowWindow As Integer
    49.   cbReserved2 As Integer
    50.   lpReserved2 As Byte
    51.   hStdInput As Long
    52.   hStdOutput As Long
    53.   hStdError As Long
    54. End Type
    55.  
    56. Private Type OVERLAPPED
    57.     ternal As Long
    58.     ternalHigh As Long
    59.     offset As Long
    60.     OffsetHigh As Long
    61.     hEvent As Long
    62. End Type
    63.  
    64. Private Const STARTF_USESHOWWINDOW = &H1
    65. Private Const STARTF_USESTDHANDLES = &H100
    66. Private Const SW_HIDE = 0
    67. Private Const EM_SETSEL = &HB1
    68. Private Const EM_REPLACESEL = &HC2
    69.  
    70. Private Sub Command1_Click()
    71.   Command1.Enabled = False
    72.   Redirect Text1.Text, Text2
    73.   Command1.Enabled = True
    74. End Sub
    75. Private Sub Form_Load()
    76.     Text1.Text = "ping"
    77. End Sub
    78. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    79.   If Command1.Enabled = False Then Cancel = True
    80. End Sub
    81.  
    82. Sub Redirect(cmdLine As String, objTarget As Object)
    83.   Dim i%, t$
    84.   Dim pa As SECURITY_ATTRIBUTES
    85.   Dim pra As SECURITY_ATTRIBUTES
    86.   Dim tra As SECURITY_ATTRIBUTES
    87.   Dim pi As PROCESS_INFORMATION
    88.   Dim sui As STARTUPINFO
    89.   Dim hRead As Long
    90.   Dim hWrite As Long
    91.   Dim bRead As Long
    92.   Dim lpBuffer(1024) As Byte
    93.   pa.nLength = Len(pa)
    94.   pa.lpSecurityDescriptor = 0
    95.   pa.bInheritHandle = True
    96.  
    97.   pra.nLength = Len(pra)
    98.   tra.nLength = Len(tra)
    99.  
    100.   If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
    101.     sui.cb = Len(sui)
    102.     GetStartupInfo sui
    103.     sui.hStdOutput = hWrite
    104.     sui.hStdError = hWrite
    105.     sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
    106.     sui.wShowWindow = SW_HIDE
    107.     If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then
    108.       SetWindowText objTarget.hwnd, ""
    109.       Do
    110.         Erase lpBuffer()
    111.         If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
    112.           SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
    113.           SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
    114.           DoEvents
    115.         Else
    116.           CloseHandle pi.hThread
    117.           CloseHandle pi.hProcess
    118.           Exit Do
    119.         End If
    120.         CloseHandle hWrite
    121.       Loop
    122.       CloseHandle hRead
    123.     End If
    124.   End If
    125. End Sub

    I also think that Joacim had posted some better code than this.


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sending text to cmd.exe?

    So SendMessage will only work if you create the pipe first? I know it doesnt work directly by getting the CMD window handles.
    I have read on MSDN somewhere that the message stream is inaccible directly.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286

    Re: sending text to cmd.exe?

    Quote Originally Posted by RobDog888
    So SendMessage will only work if you create the pipe first? I know it doesnt work directly by getting the CMD window handles.
    You shouldn't have to bring pipes into it

    The following works for me
    VB Code:
    1. Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    2. Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    3. Private Const WM_CHAR As Long = &H102
    4.  
    5. Private Sub Command1_Click()
    6.  
    7.     Dim h As Long
    8.    
    9.     h = FindWindowEx(0, 0, "ConsoleWindowClass", vbNullString)
    10.     If h Then PostMessage h, WM_CHAR, Asc("a"), 0
    11.  
    12. End Sub

  8. #8
    Hyperactive Member Datacide's Avatar
    Join Date
    Jun 2005
    Posts
    309

    Re: sending text to cmd.exe?

    What's wrong with doing it this way?
    VB Code:
    1. Private Sub cmdSendCommand_Click()
    2.   Shell(txtCommand.text)
    3. End Sub
    PHP in your FACE!

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sending text to cmd.exe?

    Ah, so it does work with PostMessage. How would you do it with SendMessage? WM_SETTEXT?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286

    Re: sending text to cmd.exe?

    Rob: Since we're sending WM_CHAR directly, it will work with SendMessage too. If however, we tried to send WM_KEYDOWN and WM_KEYUP, it would fail, since it bypasses the message queue, and thus, the call to TranslateMessage. (But the latter will work with PostMessage).

    Short answer: WM_CHAR with either Post/SendMessage will do the trick.

    Datacide: Nothing wrong with that (in fact, that's what Rob first suggested). I was just showing another method you can use, if the window is already open.
    Last edited by Megatron; Aug 29th, 2005 at 08:11 PM.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sending text to cmd.exe?

    Thanks Megatron. That really clears things up for me. I thought I was loosing my mind.
    Will it work with the WM_SETTEXT so you can send a string instead of having to send each char?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286

    Re: sending text to cmd.exe?

    Well remember that the command prompt is "all one window" (I.e. the main window is not separated from the text entry part, like Notepad is) so sending WM_SETTEXT will only change the window's titlebar text.

    To send an entire string, you could write a function that cycles through each character in the string, and send a WM_CHAR message for each letter.

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sending text to cmd.exe?

    I was hoping there was a way to do it by string instead of char but at least I know there is a way now. That make a little more sense now Mega. Thanks
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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