Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

urgent====> displaying in command line

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
google_man
Asked:
google_man
  • 6
  • 4
  • 4
1 Solution
 
sunnycoderCommented:
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
 
google_manAuthor Commented:
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
 
sunnycoderCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
google_manAuthor Commented:
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
 
sunnycoderCommented:
> 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
 
mem_famCommented:
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
 
mem_famCommented:
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
 
sunnycoderCommented:
>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
 
mem_famCommented:
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
 
mem_famCommented:
google_man,
Any news? Have you succeeded?
0
 
google_manAuthor Commented:
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
 
mem_famCommented:
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
 
google_manAuthor Commented:
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
 
mem_famCommented:
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now