473,386 Members | 1,846 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,386 software developers and data experts.

communicating with a console application

Hi,

I'm trying to communicate with a console application through a c# program.
the console application is micq a console based ICQ client.
I want to be able to send an receive messages through the micq and then act upon them.

I've been using the System.Diagnostics.Process class and redirecting the input and output stream. R
eading the output works fine, but I am not able to send any commands to the application. Or better, micq doesn't act upon the commands I send him.

See the code below for more detail.
I would be really glad, if anyone could help me. I'm goin mad already. Oh and I'm not a very good programmer, so excuse me if the code isn't neat! :)

Oh I use a new threat to read the output, because the ReadLine() command freezes if it reaches the end of the console output..

----------------------------------code----------------------------------------
Expand|Select|Wrap|Line Numbers
  1. using System.Data;
  2. using System.Drawing;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Threading;
  6. using System.IO;
  7.  
  8.  
  9. namespace test6
  10. {
  11.  
  12.  
  13.     public partial class Form1 : Form
  14.     {
  15.         static System.Diagnostics.Process p;
  16.         static string test;
  17.         static System.IO.StreamWriter mystreamwriter ;
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.  
  23.  
  24.             p = new System.Diagnostics.Process();
  25.             p.StartInfo.FileName = "cmd.exe";
  26.             p.StartInfo.UseShellExecute = false;
  27.             p.StartInfo.RedirectStandardOutput = true;
  28.             p.StartInfo.RedirectStandardInput = true;
  29.             p.StartInfo.CreateNoWindow = false;
  30.             p.StartInfo.StandardOutputEncoding = Encoding.ASCII;
  31.  
  32.         }
  33.  
  34.         ~Form1()
  35.         {
  36.             p.StandardInput.WriteLine("exit");
  37.         }
  38.  
  39.         private void startbtn_Click(object sender, EventArgs e)
  40.         {
  41.             p.Start();
  42.             label1.Text = "IDLE";
  43.  
  44.             mystreamwriter = p.StandardInput;
  45.             mystreamwriter.WriteLine("micq -u ICQNUMBER -p PASSWORD -c");
  46.             mystreamwriter.AutoFlush = true;
  47.  
  48.         }
  49.  
  50.         private void pwdbtn_Click(object sender, EventArgs e)
  51.         {
  52.             //p.StandardInput.WriteLine("think960");
  53.             //mystreamwriter.Write("");
  54.         }
  55.  
  56.         private void sendbtn_Click(object sender, EventArgs e)
  57.         {
  58.             mystreamwriter.WriteLine("msg 12345678 test-von-c#");
  59.             mystreamwriter.Flush();
  60.             mystreamwriter.Close();
  61.  
  62.         }
  63.  
  64.         private void Form1_Load(object sender, EventArgs e)
  65.         {
  66.  
  67.         }
  68.  
  69.         private void getoutputbtn_Click(object sender, EventArgs e)
  70.         {
  71.             test = "";
  72.  
  73.             getoutput workerObject = new getoutput();
  74.             Thread workerThread = new Thread(workerObject.DoWork);
  75.  
  76.             // Start the worker thread.
  77.             workerThread.Start();
  78.             //test +="main thread: Starting worker thread...";
  79.  
  80.             // Loop until worker thread activates.
  81.             while (!workerThread.IsAlive) ;
  82.  
  83.             // Put the main thread to sleep for 1 millisecond to
  84.             // allow the worker thread to do some work:
  85.             Thread.Sleep(5);
  86.  
  87.             // Request that the worker thread stop itself:
  88.             workerObject.RequestStop();
  89.             label1.Text += test;
  90.  
  91.         }
  92.  
  93.         private void getcharbtn_Click(object sender, EventArgs e)
  94.         {
  95.  
  96.  
  97.         }
  98.  
  99.         public class getoutput
  100.         {
  101.             // This method will be called when the thread is started.
  102.             public void DoWork()
  103.             {
  104.                 while (!_shouldStop)
  105.                 {
  106.                     string sOutput;
  107.                     sOutput = p.StandardOutput.ReadLine();
  108.                     test += "\n" + sOutput;
  109.                 }
  110.                 test += "worker thread: terminating gracefully.";
  111.             }
  112.             public void RequestStop()
  113.             {
  114.                 _shouldStop = true;
  115.             }
  116.             // Volatile is used as hint to the compiler that this data
  117.             // member will be accessed by multiple threads.
  118.             private volatile bool _shouldStop;
  119.         }//class getoutput
  120.  
  121.         private void button1_Click(object sender, EventArgs e)
  122.         {
  123.             p.StandardInput.Write("exit\n");
  124.             p.Close();
  125.         }
  126.  
  127.  
  128.     }//Form1
  129.  
  130.  
  131. } // namespace
  132.  
  133.  
----------------------------------------------------------------------------------
Aug 9 '07 #1
6 1698
Plater
7,872 Expert 4TB
Since you are starting with cmd.exe I believe that when you tell it to run micq it's actually making a new process(or close enough) and it's getting it's own set of in/out streams.

You could try starting micq as a process itself instead of cmd.exe
Aug 9 '07 #2
Hi,

yeah I've tried that already... I've tried pretty much everything I could think of, it just doen't want to work....

If I run the process with createnowindow = false,
It opens a console like window, with no output. I guess that is because the output is being redirected.
But the weird part is, that if I click into that window and type a command for micq, micq actually executes that command, but through my C# programm it doesn't?!

Any Ideas?
Aug 9 '07 #3
Plater
7,872 Expert 4TB
Hmm which port of mICQ do you have? I saw a cygwin and a mingw.
Neither looked to be very windows native.
I'm not sure what to tell you except maye to try and redirect only one of the streams at a time and see if you can control that?
Aug 9 '07 #4
I have been using the MinGW micq

and yeah I think they were made for linux. you think that could be the problem?
I've read about similar problems in another forum, but there was no real solution

Oh, and I also already tried only redirecting one stream...

Mike
Aug 9 '07 #5
Just in case, someone cares... :)

I got it to work with the other version of micq

Mike
Aug 9 '07 #6
Plater
7,872 Expert 4TB
really? the cygwin version worked?
(cygwin is an attempt at a linux port for windows)
Aug 10 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: David Dolheguy | last post by:
I have written an application using VB.Net 2003 which contains a single timer control (Interval set to 5 seconds) and a function which uses the BeginReceiveFrom Socket function. The problem I am...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
3
by: Stan | last post by:
Hallo, I have developed an application in MS Access 2000 (Polish version) under MS Windows XP prof (also Polish). Now I would like to run this code on MS Windows XP EN and MS Access XP EN. I have...
2
by: Matthew Playne | last post by:
Hi, I am having a problem communicating between to classes. I have an MDI application. The parent window contains a mainMenu and a toolbar and toolbuttons, and the child form contains a...
6
by: Mark Allison | last post by:
Hi, I have an application that I want to be to run in Console mode and GUI mode. If no params are entered, I want the GUI fired up, if params are entered, then go into console mode. I believe...
5
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...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
1
by: bhargavchokshi | last post by:
Hi, I m kind of new to multithreading programming and try to implement one solution. The problem I have is, I have one background thread which does time consuming processing. The parent(calling)...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.