|
-
Feb 6th, 2009, 05:19 AM
#1
Thread Starter
Junior Member
Sending Keys to a Process??
Hi,
I have an external exe file that runs like a console aplication (CLI). It is started as a process and is embedded in a Panel on a form.
I can type commands directly at its prompt, but I would also like to send it keystrokes from a button.
Here is the code that starts it, embeds it into a Panel, removes its TitleBar, and positions and sizes it to the size of the Panel.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim p As New System.Diagnostics.ProcessStartInfo
p.FileName = "C:\Program Files\Console2\Console.exe"
proc.StartInfo = p
proc.Start()
proc.WaitForInputIdle()
SetParent(proc.MainWindowHandle, Panel1.Handle)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, 0)
InitialStyle = GetWindowLong(proc.MainWindowHandle, GWL_STYLE)
SetWindowLong(proc.MainWindowHandle, -16, InitialStyle And Not WS_DLGFRAME)
MoveWindow(proc.MainWindowHandle, Panel1.Left, Panel1.Top, Panel1.Width, Panel1.Height, True)
End Sub
I wondered if SendKeys would work, but the documentation says it should be sent to the 'active window', but there I get confused, as my exe is a process, and I do not know if the Panel it is embedded into is the 'window' referred to, or the proc.MainWindowHandle, and I do not know whether it is active or not.
How should I go about this?
Edit: Just tried this, but I get an error at the AppActivate line: Process '{0}' was not found, even though the MessageBox correctly reports that the Window Title of the process is "Console".
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim p As New System.Diagnostics.ProcessStartInfo
p.FileName = "C:\Program Files\Console2\Console.exe"
pproc.StartInfo = p
proc.Start()
proc.WaitForInputIdle()
SetParent(proc.MainWindowHandle, Panel1.Handle)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, 0)
InitialStyle = GetWindowLong(proc.MainWindowHandle, GWL_STYLE)
SetWindowLong(proc.MainWindowHandle, -16, InitialStyle And Not WS_DLGFRAME)
MoveWindow(proc.MainWindowHandle, Panel1.Left, Panel1.Top, Panel1.Width, Panel1.Height, True)
MsgBox(proc.MainWindowTitle)
AppActivate(proc.MainWindowTitle)
SendKeys.SendWait("dir" & "{Enter}")
End Sub
Any ideas?
TIA
Rock
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|