473,832 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

communicating with a console application

4 New Member
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.Diagnost ics.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 1726
Plater
7,872 Recognized Expert Expert
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
dolulob
4 New Member
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 Recognized Expert Expert
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
dolulob
4 New Member
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
dolulob
4 New Member
Just in case, someone cares... :)

I got it to work with the other version of micq

Mike
Aug 9 '07 #6
Plater
7,872 Recognized Expert Expert
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
970
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 having is that everytime the timer runs and calls the function which sends and receives data through the UDP protocol, it increases the systems thread count by 1. After the software running for around 1 hour windows cracks it as I am using too...
1
5389
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
3
23886
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 converted the mdb to version 2002 under MS Access XP Polish and under this version everything works OK. The problem starts when I copy the mdb file to Windows XP EN and start it with MS Access XP EN. I get following error: "The expression On...
2
1439
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 richTextBox. What I am trying to do is to when a the selected text in the richTextBox is bullet indented, a menu option on the parent window is toggled on or off depending on what is
6
3238
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 I have all the code set up to do this, however when I issue a Console.WriteLine instruction, nothing gets written to the console. In fact, the command prompt comes back before the program has finished executing. What do I need to do?
5
11267
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
17
4243
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 console using System.Console.WriteLine if thge project has its "Output Type" to "Windows Application". However I can write to stdio if i set output type to "Console Application". When I do this I unfortunately get a "console box" as well
1
1694
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) thread can be windows application or console application or simple object. How can I pass message from Background thread to parent thread. The message is of type custom object so that I cant use BackgroundWorker class's progress report method. ...
12
6552
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 combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned Console.ReadLine() statement.
0
9642
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
10780
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...
0
10498
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10540
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
9319
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6951
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
5623
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...
1
4421
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
2
3970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.