473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Send keystrokes to runing process

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()
Sep 22 '06 #1
1 3777
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/micro...0e6a?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/subscripti...#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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris | last post by:
Hello Everyone, How can I/Is it possible with VB.NET create a service that can send keystrokes to another running process that is not a .NET exe? TIA
5
by: Go Perl | last post by:
Hello Here is an interesting question which I could not solve it. I have an old dos app for which source code is lost. I just have the exe file. So I have created a front-end for that. I...
3
by: Wayne | last post by:
I have a console application (ssh2.exe) that requires keystrokes to be sent to it after activating. I have worked that it needs to be started in it own thread, but capturing the process and...
5
by: Lucas Tam | last post by:
Hi all, Has anyone done this before? Send client side keystrokes to an application from a webpapge? This maybe more of a Javascript question. In short, I have an application on the client's...
6
by: Gary | last post by:
Hi, I am trying to use the "System.Windows.Forms.SendKeys" class for triggering the Ctrl+P key. Syntax: System.Windows.Forms.SendKeys.Send("^(P)") This is not working ..what could be the...
3
by: Gunnar Syren | last post by:
I'm trying to implement a macro feature in my application by recording and playing back keystrokes. At first I thought it would be enough to catch KeyDown in my main form, but I soon realized that...
0
by: srinathava | last post by:
Hi, I am trying to send keystrokes to the NES emulator nester. The idea was to be able to use a standard NES controller whose "state" I can read in using Python and then conveying that to the...
3
by: =?Utf-8?B?QmVybmFyZG8gU2FsYXphciBuZXdi?= | last post by:
Hi everybody... i need help with this issue: i have a textbox control with the focus. at side, i have a datagridview control populated with some data. i need to control behavior of dgv control...
0
by: alan75 | last post by:
I wanted to send keystrokes to a game using a vb.net form. The game is in windowed mode, not fullscreen. All I have at the moment are these: AppActivate("Name of My Game") SendKeys.Send("i") ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.