How to send a message to a hidden cmd shell

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IronRazer
    New Member
    • Jan 2013
    • 83

    How to send a message to a hidden cmd shell

    I am running a process in my GUI program that starts ffmpeg in a hidden cmd shell. I need to make it so when i press the STOP button on my GUI it sends a key stroke message to the cmd shell as if i pressed the "q" key which would tell ffmpeg to stop. I can`t use the Process.Kill() command because it messes up the file that ffmpeg is making.

    I found some examples of sending messages to other windows made with VB6 :

    Code:
            Dim lNotepadHwnd As Long
            Dim lNotepadEdit As Long
            Dim sMsg As String = "q"
            lNotepadHwnd = FindWindow("Notepad", vbNullString)
            lNotepadEdit = FindWindowEx(lNotepadHwnd, 0&, "Edit", vbNullString)
            SendMessageSTRING(lNotepadEdit, WM_SETTEXT, 256, sMsg)
    I get the SendMessage and FindWindow is not declared error in my VB 2008 program. It looks more like C++ to me but, can someone tell me why I get this error or can someone show an example of how to send a message to a hidden cmd shell in VB 2008 ?

    I can get as far as getting the handle of the process but i cant find what i need to send the message.

    This is my Stop button code :

    Code:
        Private Sub Btn_Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Stop.Click
            Dim processHandle As Long = Process1.Handle
    
            'What type of command do i need hear to send "q" key to hidden cmd shell ?
    
            Btn_Stop.Enabled = False
            Btn_Record.Enabled = True
            UD_Minutes.Enabled = True
        End Sub
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    Check out the SendKeys Class, it may help you accomplish what you're looking for.

    Also, moving to VB.NET forum so more VB.NET experts can see your question :)

    Comment

    • IronRazer
      New Member
      • Jan 2013
      • 83

      #3
      First i would like to say Thanks For helping me. After checking out the link you gave, i stumbled onto a lot of stuff from there that helped me get a lot farther than i was. I now have the following that works if i have the Process running in a normal or minimized window state.

      This is what i have added to get it to work so far:
      Code:
      Imports System.Runtime.InteropServices
      
          Dim hWnd As IntPtr
      
          <DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
          Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
          End Function
      
          <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Long
          End Function
          
          Private Sub Btn_Record_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesBtn_Record.Click
              Process1.Start()
          End Sub
      
          Private Sub Btn_Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Stop.Click
              hWnd = FindWindow(Nothing, Process1.MainWindowTitle)
              SetForegroundWindow(hWnd)
              SendKeys.Send("q")
          End Sub
      I would really like to have the Process running in a hidden window state. Is it possible to use SendMessage, PostMessage, or maybe SendInput to send a KeyDown and KeyUp to the Process if it is running in a hidden window state or is it just not possible?

      PS. I realized yesterday that it was in the wrong VB Forum and tried re-posting it in VB.NET and it got removed. I will see if i can figure out how to move it.

      Comment

      Working...