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("kern el32", SetLastError = true)]
public static extern bool AttachConsole(u int dwProcessId);
[DllImport("kern el32.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.GetProc essesByName("cm d");
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.ToUInt3 2(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(p ID);
//Perform our writes
Console.WriteLi ne("DIR");
//Detach from the console
FreeConsole();
}
}
}
"ME" <tr*********@co mcast.netREMOVE THIS> wrote in message
news:zY******** ************@co mcast.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