Solved

urgent====> displaying in command line

Posted on 2003-12-05
16
582 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now