Link to home
Start Free TrialLog in
Avatar of weloytty
weloytty

asked on

Write to the console that started app from GUI app

System: P150,80MB RAM, NT4.0SP3
I have a CLI program that execs a GUI application.
Just for grins, I want to write to the console that
the GUI application started from.
I set the startupinfo structure like:
      si.hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
      si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
      si.hStdError=GetStdHandle(STD_ERROR_HANDLE);
      si.dwFlags = STARTF_USESTDHANDLES;
      
I also set bInheritHandles in both the Thread's
SECURITY_ATTRIBUTE and the Processes' SECURITY_ATTRIBUTE
to TRUE.
I call CreateProcess, and the GUI app gets the proper
handles, and when I call WriteConsole (or WriteFile),
and then call FlushFileBuffers (or fflush, or
FlushConsoleInputBuffer or no flush calls) the function
returns properly (non-zero) and the number of characters
written is returned, but I get nothing on the console.

According to the SDK docs,
(mk:@ivt:pdref/native/sdk/win32/func/src/f25_7.htm)
console output is not buffered in WinNT, so I am pretty
sure I dont need FlushFileBuffers, but it was a good try.

Is this possible?
I know that it isnt that important, but it is driving
me crazy.
Help!
Avatar of byang
byang
Flag of United States of America image

First, try flush the buffers. Make the GUI program a child process of CLI program. Make sure the handles are inheritable by the GUI program.
Avatar of weloytty
weloytty

ASKER

Edited text of question
Thanks for the input, but alas, fflush, FlushFileInput, and FlushConsoleInputBuffer didnt work.  The SDK docs say console output is not buffered in NT.Thanks for the try, though.
Edited text of question
One more thing to try: use DuplicateHandle() to create handles for the GUI program.


ASKER CERTIFIED SOLUTION
Avatar of NickRepin
NickRepin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No, doesn't AllocConsole() gives the GUI app a new console ? And it's not related to the CLI console.

I think DuplicateHandle() on the CLI-side, and SetStdHandle() on the GUI-side are needed.
AllocConsole gives GUI app the new console.
AllocConsole gives GUI app the new ordinary console (like any over console, eg MSDOS). And you can read/write to it without any problem.

There are terms 'attached console' in MS SDK. Do they mean that it's not enough to GUI app to have parent's console handles, but also be attached to console? I think, yes.

That's MS SDK say:
'A process can use the DuplicateHandle function to create a duplicate console handle that has different access or inheritability from the original handle. Note, however, that a process can create a duplicate console handle only for its OWN use. This DIFFERS from other handle types (such as file, pipe, or mutex objects), for which DuplicateHandle can create a duplicate that
is valid for a different process.'

By the way, what is CLI? I guess, it's console-mode application?

CLI stands for Command Line Interface, as opposed to GUI.

Thanks, that's exactly what I mean.
I have pretty much come to the conclusion that it can't be done. I have implemented a fake--I use a memory mapped file to pass stuff back and forth, and it LOOKS like it works, so I got what I wanted, I just didnt get to do it the way that I wanted to.Thanks,