468,284 Members | 1,525 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,284 developers. It's quick & easy.

Attach to Console?

ME
I have a application that someone else has wrote that runs in a console
window. It does not take parameters, but when running it asks three
questions and then exits. I would like to write a small utility to attach
to the console app, send the necessary answers (typically typing the letter
"y" and enter) and then leave. Is this a possiblity for .net? If so what
would be the best approach?

Thanks,

Matt
Apr 25 '06 #1
9 6585
Is redirection acceptable?

Create a file named foo.inp, then do

conapp.exe < foo.inp
Apr 25 '06 #2
ME
Unfortunately no. The console app is the first part of a larger application
that starts up a custom web server after answering some questions. I wish
it were that easy. Basically I have no control of the console app until it
asks the first question.

Thanks,

Matt
"lgs.lgs" <li************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Is redirection acceptable?

Create a file named foo.inp, then do

conapp.exe < foo.inp

Apr 25 '06 #3
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:zY********************@comcast.com...
I have a application that someone else has wrote that runs in a console
window. It does not take parameters, but when running it asks three
questions and then exits. I would like to write a small utility to attach
to the console app, send the necessary answers (typically typing the letter
"y" and enter) and then leave. Is this a possiblity for .net? If so what
would be the best approach?


You could try using the SendKeys class.
Apr 25 '06 #4
ME
Unfortunately this didn't seem to work either. It appears SendKeys uses the
API SendMessage to transfer the keys. The CONSOLE doesn't seem to process
window messages.
"James Park" <fa******@fakeaddress.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:zY********************@comcast.com...
I have a application that someone else has wrote that runs in a console
window. It does not take parameters, but when running it asks three
questions and then exits. I would like to write a small utility to attach
to the console app, send the necessary answers (typically typing the
letter "y" and enter) and then leave. Is this a possiblity for .net? If
so what would be the best approach?


You could try using the SendKeys class.

Apr 25 '06 #5
ME
I have figured out how to ATTACH to a running console. I have yet to figure
out how to send an ENTER key, but here is what i have so far:

public class Attach
{
[DllImport("kernel32", SetLastError = true)]
public static extern bool AttachConsole(uint dwProcessId);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern bool FreeConsole();
public static void AttachToCMD()
{
//Get a list of running process that are command windows
Process[] ps = Process.GetProcessesByName("cmd");
Process p = null;
uint pID = 0;
bool retVal = false;
string inputText = string.Empty;
//if we found command windows then attach to the first one
if (ps.Length > 0)
{
p = ps[0];
}
//if we successfully attached then get uint with process id so
//can pass the id to the AttachConsole method
if (p != null)
{
pID = Convert.ToUInt32(p.Id);
}
//if our pid > 0 then attach
if (pID > 0)
{
//attach to the running console. remember that an app can only
//attach to one console at a time
retVal = AttachConsole(pID);
//Perform our writes
Console.WriteLine("DIR");
//Detach from the console
FreeConsole();
}
}
}
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:zY********************@comcast.com...
I have a application that someone else has wrote that runs in a console
window. It does not take parameters, but when running it asks three
questions and then exits. I would like to write a small utility to attach
to the console app, send the necessary answers (typically typing the letter
"y" and enter) and then leave. Is this a possiblity for .net? If so what
would be the best approach?

Thanks,

Matt

Apr 25 '06 #6
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:B5********************@comcast.com...
Unfortunately this didn't seem to work either. It appears SendKeys uses
the API SendMessage to transfer the keys. The CONSOLE doesn't seem to
process window messages.


You could also try P/Invoke'ing SendInput, which often works when
keybd_event and SendKeys doesn't.
Apr 25 '06 #7
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:B5********************@comcast.com...
Unfortunately this didn't seem to work either. It appears SendKeys uses
the API SendMessage to transfer the keys. The CONSOLE doesn't seem to
process window messages.


The following seems to work for me.

string cmdPath = Environment.ExpandEnvironmentVariables("%COMSPEC%" );
Process consoleProcess = Process.Start(cmdPath);
Thread.Sleep(2000);

SetForegroundWindow(consoleProcess.MainWindowHandl e);
SendKeys.SendWait("{ENTER}");
Apr 25 '06 #8
ME
THANK YOU! Grabbing the foreground window is what I had overlooked.

Thanks,

Matt
"James Park" <fa******@fakeaddress.com> wrote in message
news:eW**************@TK2MSFTNGP04.phx.gbl...
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:B5********************@comcast.com...
Unfortunately this didn't seem to work either. It appears SendKeys uses
the API SendMessage to transfer the keys. The CONSOLE doesn't seem to
process window messages.


The following seems to work for me.

string cmdPath = Environment.ExpandEnvironmentVariables("%COMSPEC%" );
Process consoleProcess = Process.Start(cmdPath);
Thread.Sleep(2000);

SetForegroundWindow(consoleProcess.MainWindowHandl e);
SendKeys.SendWait("{ENTER}");

Apr 25 '06 #9
>I have figured out how to ATTACH to a running console. I have yet to
figure out how to send an ENTER key, but here is what i have so far:


Doesn't neither "\r", "\n" or "\r\n" work?
Apr 26 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jason Callas | last post: by
5 posts views Thread by Simon Johnson | last post: by
3 posts views Thread by Dave | last post: by
2 posts views Thread by Rick | last post: by
7 posts views Thread by Devron Blatchford | last post: by
6 posts views Thread by Mythran | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.