Results 1 to 14 of 14

Thread: sending text into cmd.exe?

  1. #1

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

    sending text into cmd.exe?

    hello, how to send a text into cmd.exe?

    from Text1.Text: -> cmd.exe?

    br,

  2. #2
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: sending text into cmd.exe?

    I think this is possible with a window hook, although I havn't worked with window hooks personally. I'm pretty sure thats what you would have to do.
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  3. #3

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

    Re: sending text into cmd.exe?

    @k1ll3rdr4g0n

    sir, do u have an sample for that?

    Thanks...

    br,

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: sending text into cmd.exe?

    Use a program such as Spy++ (I think that does it), to get the Classname of the Command Prompt text input window. Then use the FindWindow, SendMessage and the WM_SETTEXT/WM_CHAR messages. There are many posts about how to input text into another program. Search the forums

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: sending text into cmd.exe?

    Shell "cmd " & Text1.Text

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: sending text into cmd.exe?

    That would open it with the text. I thought the original question was to add text to an already open Command Prompt?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: sending text into cmd.exe?

    I didn't, so we will have to wait for nokmaster to clarify that.

    Also, I have a feeling WM_SETTEXT probably won't work in this case because of the way consoles operate, not sure however.

  8. #8

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

    Re: sending text into cmd.exe?

    open first the cmd.exe and then in Text1.Text you input: NAME and then send it to cmd.exe

    that's what i looking....

    thanks...

  9. #9

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

    Re: sending text into cmd.exe?

    how to send from Text1.Text to cmd.exe?

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

    Re: sending text into cmd.exe?

    Sending messages to the command window will not work unless you create a pipe window via APIs so you can interact with the Windows message stream.
    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. ' dl
    7. 'VB port was made by Jernej Simoncic
    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, _
    17. lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, _
    18. ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    19. Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    20. 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
    21. 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
    22. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    23.  
    24. Private Type SECURITY_ATTRIBUTES
    25.   nLength As Long
    26.   lpSecurityDescriptor As Long
    27.   bInheritHandle As Long
    28. End Type
    29.  
    30. Private Type PROCESS_INFORMATION
    31.   hProcess As Long
    32.   hThread As Long
    33.   dwProcessId As Long
    34.   dwThreadId As Long
    35. End Type
    36.  
    37. Private Type STARTUPINFO
    38.   cb As Long
    39.   lpReserved As Long
    40.   lpDesktop As Long
    41.   lpTitle As Long
    42.   dwX As Long
    43.   dwY As Long
    44.   dwXSize As Long
    45.   dwYSize As Long
    46.   dwXCountChars As Long
    47.   dwYCountChars As Long
    48.   dwFillAttribute As Long
    49.   dwFlags As Long
    50.   wShowWindow As Integer
    51.   cbReserved2 As Integer
    52.   lpReserved2 As Byte
    53.   hStdInput As Long
    54.   hStdOutput As Long
    55.   hStdError As Long
    56. End Type
    57.  
    58. Private Type OVERLAPPED
    59.     ternal As Long
    60.     ternalHigh As Long
    61.     offset As Long
    62.     OffsetHigh As Long
    63.     hEvent As Long
    64. End Type
    65.  
    66. Private Const STARTF_USESHOWWINDOW = &H1
    67. Private Const STARTF_USESTDHANDLES = &H100
    68. Private Const SW_HIDE = 0
    69. Private Const EM_SETSEL = &HB1
    70. Private Const EM_REPLACESEL = &HC2
    71.  
    72. Private Sub Command1_Click()
    73.   Command1.Enabled = False
    74.   Redirect Text1.Text, Text2
    75.   Command1.Enabled = True
    76. End Sub
    77. Private Sub Form_Load()
    78.     Text1.Text = "ping"
    79. End Sub
    80. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    81.   If Command1.Enabled = False Then Cancel = True
    82. End Sub
    83.  
    84. Sub Redirect(cmdLine As String, objTarget As Object)
    85.   Dim i%, t$
    86.   Dim pa As SECURITY_ATTRIBUTES
    87.   Dim pra As SECURITY_ATTRIBUTES
    88.   Dim tra As SECURITY_ATTRIBUTES
    89.   Dim pi As PROCESS_INFORMATION
    90.   Dim sui As STARTUPINFO
    91.   Dim hRead As Long
    92.   Dim hWrite As Long
    93.   Dim bRead As Long
    94.   Dim lpBuffer(1024) As Byte
    95.   pa.nLength = Len(pa)
    96.   pa.lpSecurityDescriptor = 0
    97.   pa.bInheritHandle = True
    98.  
    99.   pra.nLength = Len(pra)
    100.   tra.nLength = Len(tra)
    101.  
    102.   If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
    103.     sui.cb = Len(sui)
    104.     GetStartupInfo sui
    105.     sui.hStdOutput = hWrite
    106.     sui.hStdError = hWrite
    107.     sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
    108.     sui.wShowWindow = SW_HIDE
    109.     If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then
    110.       SetWindowText objTarget.hwnd, ""
    111.       Do
    112.         Erase lpBuffer()
    113.         If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
    114.           SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
    115.           SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
    116.           DoEvents
    117.         Else
    118.           CloseHandle pi.hThread
    119.           CloseHandle pi.hProcess
    120.           Exit Do
    121.         End If
    122.         CloseHandle hWrite
    123.       Loop
    124.       CloseHandle hRead
    125.     End If
    126.   End If
    127. End Sub
    Last edited by RobDog888; Jul 30th, 2005 at 11:49 PM. Reason: Fix API dlclaration so it wraps and doesnt mess up the page too much. :D
    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

  11. #11
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: sending text into cmd.exe?

    I think penagate answered that in post #5, unless chem was right about what you are looking. Please clarify what you need. Thanks.

    EDIT: Dangit RD!! Why do people always double post me with a much better response than mine.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  12. #12

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

    Re: sending text into cmd.exe?

    anyone can add with this code?

    VB Code:
    1. x = shell("command", 0)
    2. this will create a dos session, With windows handle of "x", which will be hidden from the program user.
    3. Once created To End the session the standard "exit" can be sent to the command line.
    4. '
    5. Public Sub Dos_Send(CommandWindowName as string, SendToDos as string)
    6.  
    7.     clipboard.Clear
    8.     'clear the clipboard (obvious)
    9.     Clipboard.SetText SendToDos ' + Chr$(13)
    10.     'send the relevent key/command to the cl
    11.     '     ipboard
    12.     'note that the Chr$(13) is requried to i
    13.     '     f the "SendToDos" text is a dos command
    14.     '     or 'requires the pressing of the Enter/R
    15.     '     eturn key after the relevent string has
    16.     '     been 'sent.
    17.     AppActivate CommandWindowName
    18.     'CommandWindowName is MS-DOS Prompt, or
    19.     '     whatever appears in your task bar.
    20.     'See the AppActivate for more info.
    21.     'A good alternative is to use the handle
    22.     '     (hwind) of the dos window
    23.     SendKeys "% ep", 1
    24.     'this sends the clipboard to dos
    25.     'Now wasn't that simple?
    26. End Sub

  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 into cmd.exe?

    I wouldnt suggest SendKeys since it is flaky and depends on the window maintaining focus. If the user accidentally tabs or mouseclicks, presses a key, the focus will change and the SendKeys will fail.
    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

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: sending text into cmd.exe?

    The above sendkey code also assumes that the system menu of a command line window has the &Edit > &Paste names which might not be true for all localized version of Windows. I happens to use an English (US) version of Windows but most people in my country use a Swedish version and the SendKeys string should in that case be "% rl" (I think) or it will not work.

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