By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,641 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,641 IT Pros & Developers. It's quick & easy.

How to send a message to a hidden cmd shell

IronRazer
P: 83
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 :

Expand|Select|Wrap|Line Numbers
  1.         Dim lNotepadHwnd As Long
  2.         Dim lNotepadEdit As Long
  3.         Dim sMsg As String = "q"
  4.         lNotepadHwnd = FindWindow("Notepad", vbNullString)
  5.         lNotepadEdit = FindWindowEx(lNotepadHwnd, 0&, "Edit", vbNullString)
  6.         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 :

Expand|Select|Wrap|Line Numbers
  1.     Private Sub Btn_Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Stop.Click
  2.         Dim processHandle As Long = Process1.Handle
  3.  
  4.         'What type of command do i need hear to send "q" key to hidden cmd shell ?
  5.  
  6.         Btn_Stop.Enabled = False
  7.         Btn_Record.Enabled = True
  8.         UD_Minutes.Enabled = True
  9.     End Sub
  10.  
Jan 17 '13 #1
Share this Question
Share on Google+
2 Replies


PsychoCoder
Expert Mod 100+
P: 465
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 :)
Jan 18 '13 #2

IronRazer
P: 83
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:
Expand|Select|Wrap|Line Numbers
  1. Imports System.Runtime.InteropServices
  2.  
  3.     Dim hWnd As IntPtr
  4.  
  5.     <DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
  6.     Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
  7.     End Function
  8.  
  9.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Long
  10.     End Function
  11.  
  12.     Private Sub Btn_Record_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesBtn_Record.Click
  13.         Process1.Start()
  14.     End Sub
  15.  
  16.     Private Sub Btn_Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Stop.Click
  17.         hWnd = FindWindow(Nothing, Process1.MainWindowTitle)
  18.         SetForegroundWindow(hWnd)
  19.         SendKeys.Send("q")
  20.     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.
Jan 19 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.