Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hello, I have a console application that works with command line arguments which I don't have its source code, & I want to create a GUI for it to make it easier.

My problem is how to make the GUI application calls the console application & handle its output results to display it in a multi-line edit control with a black background & white text in the GUI application !?

Help me please

NB: Updated.
Posted
Updated 29-Mar-12 15:39pm
v2
Comments
Mohibur Rashid 29-Mar-12 20:54pm    
Do you know anything about how win32 application works?
Mr. Tomay 29-Mar-12 21:40pm    
yes I do.
Sergey Alexandrovich Kryukov 29-Mar-12 21:14pm    
How can it add any problems?
--SA
Mr. Tomay 29-Mar-12 21:40pm    
I have updated my question
Mohibur Rashid 30-Mar-12 0:00am    
I am not sure if you can do; what I think you want to do

1) The first part is to call your console application from the UI application - this is easy. For example, in C++ you could call ShellExecute, or CreateProcess with correct command line arguments. I don't know what language/platform you will be using for your UI so I do not elaborate much.

2) The second part is inside the UI application - you need to to capture the output from the console application and display it in the edit control of the UI. There are a number of ways to do this, the simplest that comes to mind is to redirect the output of your console app to a text file, and then read that file. To redirect, simply use > redirector, e.g. you should invoke command line such as "ipconfig /all > tmp.txt". Clearly this is the command line that your UI app should be using to invoke the console app. Then, read tmp.txt and analyze/display.
 
Share this answer
 
v2
Comments
Mr. Tomay 30-Mar-12 14:04pm    
Genius.
Seems you have understood my question.
One problem left: How to read the "tmp.txt" from the UI while the console app is still writing to it.
michaelmel 1-Apr-12 20:23pm    
After you "kicked" the console application in the UI, you could just wait for file tmp.txt to be created by the console. When UI finished reading from file, delete tmp.txt. Then the whole thing repeats. This will work unless console continuously produces output in an asynchronous manner. If this is the case, you can still read the file, just need to use an appropriate type of file access. I think overlapped I/O should do it.
Francisco T. Chavez 5-Dec-13 21:44pm    
What about using a pipe to capture the data from console application? This way you won't have to wait for the console to finish with the temp file.
I hope I understand your issue.
Suppose the name of your console application is TestConsole.exeand it gives the output "ABCDEFGHIJKLM" on executing via command prompt.
Now You need to execute this TestConsole.exe from an application that has a GUI and get the result in your GUI application. If this is the situation then write belwo code in your GUI app
C++
char   pchBuffer[128];
FILE   *pPipe;
if( (pPipe = _popen( "TestConsole", "rt" )) != NULL)
{
    while(fgets(pchBuffer, 128, pPipe))
    {
        // pchBuffer will have the output of console application
        // so write here the code to display this in your edit control
        // or do whatever you want
    }
}
 
Share this answer
 
Comments
Mr. Tomay 30-Mar-12 14:17pm    
You are awesome.
You have completely understood my question.

I will try it.
 
Share this answer
 
Please see it[^] :)

Your parent process could be covered by a GUI application
and its ReadFromPipe(..) function could transfer the child's text into a box.
 
Share this answer
 
There is no such thing as "command line application". This is because absolutely any application can get whatever appears in the command line and analyze it. If does not really related to threads or UI.

First of all, in C/C++ applications, you can use entry points with different signatures (for, in fact, please see the link below). Two of them accept command line parameters. When such signatures are used, the runtime system takes the command line argument which OS passes to the application, parse them properly and passes as arguments of the entry point function. Here they are:
C
int main(int argc, char **argv)
int main(int argc, char *argv[])

You should use this data in your application, according to the application semantics you design.

For more detail, please see:
http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B[^].

As to the threads, you should use the regular precautions when passing data between threads. You need to know it anyway; there is nothing specific to passing command line arguments.

[EDIT: an answer after OP clarification of the question]

This is a totally different story. You should not call it "command line application". This is probably a "console application". You can solve this problem.

You can run the console application using System.Diagnostics.Process.Run with redirected streams StandardInput, StandardOutput and, just in case, StandardError. Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

You can find code samples with redirection here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

You don't have to read the output of your console application to the end. You could read it line by line (or some other portion, depending on application) and process it on the fly. This is a blocking call, it will keep a thread in a wait state when data from the console application is not ready. Therefore, you should do it in a separate thread. You also should wait for the process termination (Process.WaitForExit) in a separate thread. It majorly depends on what kind of console application is that. If could be prolonged process, so you will need to feed it data permanently until you decide to terminate it, and process the data coming form this child process on the fly. This would be a kind of simulation of the interactive user by your UI process.

If this is a short-leaving process and if you want to run the whole process repeatedly, this is much simpler scenario. If this thing in really fast, you don't even need to use separate thread (but in general case this is always better). You might not even feed data to it through StandardInput, because in such application it is usually possible to pass all data in the very beginning in a command line; in other words, such process can be used non-interactively.

—SA
 
Share this answer
 
v2
Comments
Mr. Tomay 29-Mar-12 21:41pm    
I think that you don't understand me.
I have updated my question.
Sergey Alexandrovich Kryukov 30-Mar-12 1:46am    
Certainly, but after your clarification of the question (and only after that), the question became quite clear.
Please see the new answer above, after [EDIT]. This is a complete recipe for what you can do.
--SA
Mr. Tomay 30-Mar-12 13:45pm    
Damn, The links you provided to me concerns .Net Framework, but I work with MFC.
Sergey Alexandrovich Kryukov 30-Mar-12 18:21pm    
Oh, I'm sorry!

With C++, this is pretty much the same, but via CreateProcess.
Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx

See also related methods on this page.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900