Solved

urgent====> displaying in command line

Posted on 2003-12-05
16
584 Views
Last Modified: 2010-04-17
Hi experts,
          I am currently in a project of incorporating command line interface in to  a dilaogue based program in vc++.I am able to parse the commands, But there is one problem. I can't show the output to the command line window.I used setwindowhandle, getwindowhandle almost all functions related to getting command window handle.But in vain. Please anyone suggest me a way to get the output get buffered to command line window.I heard using a pipe to route will help. Please provide me help on this (code snippet preferred), or any other way through which I can accomplish this.

GM  
0
Comment
Question by:google_man
  • 6
  • 4
  • 4
16 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9880889
since it is CLI, wont a normal cout do it for you ?

If you have window of your own, that is, your process has a different controlling window and different one from which it was launched, in that case,
close stdout (1)
dup the input handle for other window
use cout...

anyway, in such case you will be doing something similar for stdin too
0
 

Author Comment

by:google_man
ID: 9880970
Hi sunnycoder,
                      I tried it with cout. but of no use. I want to display the output to same parent window thought whwich I gives the input. Can u please explain whether I can achieve through output piping.

GM
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9880975
can you give some more details about your program ...

Where do you implement the CLI, the same window from which your program is launched or do you create a new window and input/output commands from this new window ...
If latter, then how are you reading in commands ?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:google_man
ID: 9881096
Hi sunny,
               I implement the CLi from the same window from which I launch the program. Then through  function parsecommandline I get the commands  that I enter as a string and then process it( char* cmdLine = AfxGetApp()->m_lpCmdLine). Earlier in the program a dialogue box was shown when we execute the command. Now I put showidilaogue fasle and called  GetWindowText(GetNextWindow(GetTopWindow(AfxGetMainWnd()->GetSafeHwnd()), GW_HWNDNEXT),, where I handle the coommand line arguments, which I get a s a string from the comand window. But can't display it in the window

GM
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9881176
> I implement the CLi from the same window from which I launch the program
All you need to do to read in commands is cin and to write out output is cout ...
All the additional things that you seem to be doing is overhead

cout should display on the window unless you explicitly did a close(1) or close (stdout)
0
 

Expert Comment

by:mem_fam
ID: 9884989
VC++ help says: "The system creates a new console when it starts a console process, a character-mode process whose entry point is the main function." I think that without main() you application cannot use cout - there is none to open it! If your application is a GUI, it most probably is written with WinMain() of its own, and by default there is no console associated with it, even if you launch it from a command line. For this reason once you start a GUI application from a command line, you immediately see a command line prompt, and can key in the next command.

Probably the simplest way to achieve what you want would be to write a small console application what would invoke CreateProcess for you GUI executable, and in StartupInfo parameter would specify STARTF_USESTDHANDLES flag and set hStdOutput, hStdInput and hStdError to the handles of the console process you created. Then your GUI process would start with console handles ready, and you will be able to write any way you want - e.g. WriteFile, and probably even cout
0
 

Expert Comment

by:mem_fam
ID: 9885045
By saying "small console application" I meant an application that has its own main() - run-time library will then assign its own entry point, and will know to associate console window with the process when it starts:

#include <stdio.h>
#include <windows.h>
main () {
   STARTUPINFO s = {.., STARTF_USESTDHANDLES, ...};
   ...
   s.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
   ...
    CreateProcess ("GUI.EXE", ...., &s, ...);
   Wait (...);
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9887876
>If your application is a GUI, it most probably is written with WinMain() of its own, and by default there is no console associated
>with it,
He is building a CLI !!!! Why would he ever go in for a GUI ...
google_man if you are going in for a GUI then I am surprised ... What good sense does it make to have GUI for a CLI application

Anyway, are you having console application or windows application .. If latter, then what mem_fam makes good sense
0
 

Expert Comment

by:mem_fam
ID: 9889209
The original question says:
"I am currently in a project of incorporating command line interface in to  a dilaogue based program"

My understanding was that the guy has an existing GUI program, and wants to turn it into CLI+GUI. If this is a misunderstanding, my suggestions would not help, of course.  
0
 

Expert Comment

by:mem_fam
ID: 9947611
google_man,
Any news? Have you succeeded?
0
 

Author Comment

by:google_man
ID: 10009369
Hi mem_fam,
                    Sorry for the delay. I was out of station for almost 1 month. I watched ur comments. Can u please elaborate on "console application" that I should develop. How I should call ithat console application from the GUI application.....


Thanks in advance

GM
0
 

Expert Comment

by:mem_fam
ID: 10024584
Hello google_man.

What I meant is vice versa: create console application that will call your existing GUI app and pass its own console handle to it, like in my example:

#include <stdio.h>
#include <windows.h>
main () {
   STARTUPINFO s = {.., STARTF_USESTDHANDLES, ...};
   ...
   s.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
   ...
    CreateProcess ("GUI.EXE", ...., &s, ...);
   Wait (...);
}

You will need to always start this console application first, and it will start the GUI app. The GUI app will then be able to print in the console app's window.

Happy New Year.
0
 

Author Comment

by:google_man
ID: 10027553
Hi mem_fam,
                    I have 1 doubt. My GUI application doesn't have a main(). It 's accepting command line arguments in a string through parsecommandline function. Now Iam starting GUI thjrough msdos console. So Now If I call through console. How will it work?GUI application istelf doesn't have a console interface.....


Happy New Year(Belated)


Google_man
0
 

Accepted Solution

by:
mem_fam earned 100 total points
ID: 10269360
Sorry for the delay - I apparently missed mail notification on your question.

If you create a GUI process, pass handles to it and set STARTF_USESTDHANDLES (like in my example), the GUI process will inherit console handles form console process, and will be able to write to there.



0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question