Solved

Write to the console that started app from GUI app

Posted on 1997-05-30
12
563 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

634 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