473,806 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6949
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******** ********@TK2MSF TNGP02.phx.gbl. ..
Is redirection acceptable?

Create a file named foo.inp, then do

conapp.exe < foo.inp

Apr 25 '06 #3
"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?


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******@fakea ddress.com> wrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
"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?


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("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

Apr 25 '06 #6
"ME" <tr*********@co mcast.netREMOVE THIS> wrote in message
news:B5******** ************@co mcast.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*********@co mcast.netREMOVE THIS> wrote in message
news:B5******** ************@co mcast.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.Exp andEnvironmentV ariables("%COMS PEC%");
Process consoleProcess = Process.Start(c mdPath);
Thread.Sleep(20 00);

SetForegroundWi ndow(consolePro cess.MainWindow Handle);
SendKeys.SendWa it("{ENTER}");
Apr 25 '06 #8
ME
THANK YOU! Grabbing the foreground window is what I had overlooked.

Thanks,

Matt
"James Park" <fa******@fakea ddress.com> wrote in message
news:eW******** ******@TK2MSFTN GP04.phx.gbl...
"ME" <tr*********@co mcast.netREMOVE THIS> wrote in message
news:B5******** ************@co mcast.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.Exp andEnvironmentV ariables("%COMS PEC%");
Process consoleProcess = Process.Start(c mdPath);
Thread.Sleep(20 00);

SetForegroundWi ndow(consolePro cess.MainWindow Handle);
SendKeys.SendWa it("{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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4467
by: Jason Callas | last post by:
I am trying to figure out how to create a console window but cannot figure out how to do this. I know it can be done since the C# and VB console template projects do this but I see no code on how this is done. The only thing I can think of is that there is hidden code to spawn cmd.exe? Then they somehow direct console.read and console.write calls to the spawned process. Any thoughts (and some sample code) would be greatly appreciated.
5
11095
by: Simon Johnson | last post by:
Recently, a thread appeared which asked how to create a "catch all" method for when an exception occurs within a program. The solution given only works for Windows Forms applications, as far as i can tell, is it possible to do the same with a console application? Simon.
3
2294
by: Dave | last post by:
I created a console app in Visual Studio 2003 using C#. How to I distribute this app after I build it? It is my understanding that a C# app will run on any machine with the Net Framework installed. Do I simply copy the exe file to the machine on which the console app is to run?
5
11264
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough to avoid the overhead of writing to the console if it is not visible, eg I am running inside a WinForm application. thanks
2
1825
by: Rick | last post by:
I have a Windows exe app that runs with or without user intervention. When it is running w/o user intervention the program is called from a DOS windows, it's main form is set to visible=false and as it runs, I want to write out certain events to the console. I've added System.Console.WriteLine(mymessage) code, but the messages do not appear in the DOS window where the program is running. Any suggestions? Thanks, Rick
7
5076
by: Devron Blatchford | last post by:
Hi there, I am trying to develop a small menu application to run on an RF network, we are using a wavelink server to communicate with our rf scanners and run VB.NET applications on our server that repond to the scanner requests. I would like to know if there is a way that I can spawn a process on the server and attach it to the current thread (allow it to take control of the server requests)? Once this spawned app exits it will return...
1
343
by: Floris | last post by:
I was wondering if it is possible to redirect the in/output of an existing (running) console application. I have been playing around with the Process class, and the start event arguments. It works then, however, to keep it short, I'm not able to start the process from within my application. So I found the process in the GetProcesses list, and assigned an Process object to it, but I am unable to attach a StreamReader or Writer to it. ...
5
3866
by: Tim Werth | last post by:
I have a .NET console application that is kicked off by a .NET Windows service. They communicate via .NET Remoting, although there isn't much communication between the two while the console app is running. There can be, and are typically, several instances of the console app running at the same time communicating back to the service. The console app creates a delegate to do some work while the main thread waits on a ManualResetEvent to...
6
6208
by: Mythran | last post by:
Is it possible to attach Windows WndProc hooks into a Console application window? Thanks, Mythran
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10371
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.