Solved

Write to the console that started app from GUI app

Posted on 1997-05-30
12
558 Views
Last Modified: 2013-12-03
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!
0
Comment
Question by:weloytty
  • 4
  • 4
  • 4
12 Comments
 
LVL 3

Expert Comment

by:byang
ID: 1397757
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.
0
 

Author Comment

by:weloytty
ID: 1397758
Edited text of question
0
 

Author Comment

by:weloytty
ID: 1397759
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.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:weloytty
ID: 1397760
Edited text of question
0
 
LVL 3

Expert Comment

by:byang
ID: 1397761
One more thing to try: use DuplicateHandle() to create handles for the GUI program.


0
 
LVL 15

Accepted Solution

by:
NickRepin earned 200 total points
ID: 1397762
I am afraid, you cannot write to parent's console because 'GUI processes are not attached to any console when they are created'.
You can invoke GetConsoleTitle in GUI app, and it will return error code 6 (invalid handle).

The only way to create the console for GUI app is calling to AllocConsole().

0
 
LVL 3

Expert Comment

by:byang
ID: 1397763
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.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397764
AllocConsole gives GUI app the new console.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397765
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?

0
 
LVL 3

Expert Comment

by:byang
ID: 1397766
CLI stands for Command Line Interface, as opposed to GUI.

0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397767
Thanks, that's exactly what I mean.
0
 

Author Comment

by:weloytty
ID: 1397768
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,
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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