Solved

Write to the console that started app from GUI app

Posted on 1997-05-30
12
560 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Suggested Solutions

Title # Comments Views Activity
Windows Standards for file storage. 3 38
Problem to file 3 99
Visual Studio: built-in keystroke automation 2 88
Input parameteres to DragOver 2 58
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 shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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