Send keystrokes to runing process

G

Guest

I am using the below code to run a dos based program. While it is running
you can hit ctrl-c to cancel the process. Does anyone know how I can send
the same keystroke to the already runing process after the below code is
executed?

Dim process As New Process()
Dim FileName As String = "externalprog.exe"
Dim Arguments As String

Arguments = "/c"
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardOutput = True
process.StartInfo.RedirectStandardError = True
process.StartInfo.CreateNoWindow = True
process.StartInfo.FileName = FileName
process.StartInfo.Arguments = Arguments
process.StartInfo.WorkingDirectory = Application.StartupPath
process.Start()
Return process.StandardOutput.ReadToEnd()
 
J

Jeffrey Tan[MSFT]

Hi Joey,

Based on my understanding, your VB.net application uses Process class to
start another console based application. You wanted to find a way to
generate the console application programmatically just like pressing Ctrl+C
for the console window.

For console application, the Ctrl+C and CTRL+BREAK are 2 special events,
which is not received through the console application's standard input. So
it is impossible to simulate the Ctrl+C effect through standard input
redirect. The official way of generating these 2 events is using
GenerateConsoleCtrlEvent win32 API. However, there is an important
limitation in this API which makes the situation much complex, from MSDN:
"Only those processes in the group that share the same console as the
calling process receive the signal."

So our .Net application must have a console stub window to share the same
console with the application we started. Also, our console stub appliation
must start the application without CREATE_NEW_CONSOLE in CreateProcess which
is encapsulated in .Net Process class.

While .Net Process class always uses CREATE_NEW_CONSOLE(0x10) flag to call
CreateProcess, the new lauched process will be openned in a new console
window, which does not meet the GenerateConsoleCtrlEvent API requirement.

So if you use GenerateConsoleCtrlEvent API with Process class, you will have
no lucky.
The only way is p/invoke win32 CreateProcess API manually, and do not
specify CREATE_NEW_CONSOLE(0x10) flag. Then the new lauched process will
share the same console as the calling process.

I have provided a working code snippet in the thread below:
http://groups.google.com/group/micr...nguages.csharp/msg/ad9f64840af60e6a?hl=en-US&

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support Engineer
within 1 business day is acceptable. Please note that each follow up
response may take approximately 2 business days as the support professional
working with you may need further investigation to reach the most efficient
resolution. The offering is not appropriate for situations that require
urgent, real-time or phone-based interactions or complex project analysis
and dump analysis issues. Issues of this nature are best handled working
with a dedicated Microsoft Support Engineer by contacting Microsoft Customer
Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top