Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GUI or not to GUI - Determine if a window is a console window

101 views
Skip to first unread message

Thomas Steinbach

unread,
Sep 29, 2009, 6:38:58 PM9/29/09
to
Hello,

how can I proof if a program (which has WinMain and not main)
is started from a console window or if a programm is started
by a shortcut or by a doubleclick in Windows Explorer, etc.

I think if the programm is started I can use GetParent to get the
parents window handle, but then I have to test if this Window is
a console. How can I do this?

2nd)
If the parent window (which started the app) is a console,
how can I write to that console?

I tried this:

bCon = AttachConsole(ATTACH_PARENT_PROCESS);
SetConsoleTitle(_T("This is a console window") );

and the Title of the console changed to that c string, but how can
I write to that console? This:

hCon = GetConsoleWindow();
WriteConsole(hCon, _T("testing console"), _tcslen(_T("testing console")),
&dwChW, NULL);

doesn't work for me.


Thomas

Markus Schaaf

unread,
Sep 29, 2009, 6:50:51 PM9/29/09
to
Thomas Steinbach wrote:

> bCon = AttachConsole(ATTACH_PARENT_PROCESS);
> SetConsoleTitle(_T("This is a console window") );
>
> and the Title of the console changed to that c string, but how can
> I write to that console? This:
>
> hCon = GetConsoleWindow();
> WriteConsole(hCon, _T("testing console"), _tcslen(_T("testing console")),
> &dwChW, NULL);

`GetConsole` returns a window handle. What you need is a file handle:

hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

Markus Schaaf

unread,
Sep 29, 2009, 7:02:31 PM9/29/09
to
Thomas Steinbach wrote:

> how can I proof if a program (which has WinMain and not main)
> is started from a console window or if a programm is started
> by a shortcut or by a doubleclick in Windows Explorer, etc.
>
> I think if the programm is started I can use GetParent to get the
> parents window handle, but then I have to test if this Window is
> a console. How can I do this?

Why not simply call `AttachConsole(ATTACH_PARENT_PROCESS)`? It's funny
that you posted this solution already.

Thomas Steinbach

unread,
Sep 30, 2009, 5:52:45 AM9/30/09
to
Hello Markus,

>> [...]


>> hCon = GetConsoleWindow();
>> WriteConsole(hCon, _T("testing console"), _tcslen(_T("testing console")),
>> &dwChW, NULL);
>
> `GetConsole` returns a window handle. What you need is a file handle:
> hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

Perfect. Thank you.

The text is now written to the console, but the the blinking cursur remains
and I have to press Return to get the prompt back.

I use FreeConsole() to finisch my WriteConsole operations, but
that give not the console prompt back. How can I solve this?
I don't find an another appropriate function like FreeConsole()
And if I write "\r" in my last string it doesn't work for me.

Thomas

Thomas Steinbach

unread,
Sep 30, 2009, 5:57:31 AM9/30/09
to
Hello Markus,

>> [...]


>> I think if the programm is started I can use GetParent to get the
>> parents window handle, but then I have to test if this Window is
>> a console. How can I do this?
>
> Why not simply call `AttachConsole(ATTACH_PARENT_PROCESS)`? It's funny
> that you posted this solution already.

yes, that's working, but in my GUI program (WinMain) I would
determine if the app is started from a Console or if it was started from
any other program/explorer/shortcut, etc.

Because:
If the app is started from console with -help I print out the syntax to
console
(working, see previous posting) and if it is started in any other way
I show the Syntax in a MessageBox. Also working, but I have to
know if it is started from console or not...

So I have to test if the parent process is a concole. Or an another
mechanism
to test that.

Thomas

Markus Schaaf

unread,
Sep 30, 2009, 9:53:15 AM9/30/09
to
Thomas Steinbach wrote:

> So I have to test if the parent process is a concole. Or an another
> mechanism
> to test that.

Did you notice that `AttachConsole` has a return value? If the parent
does not have a console, I would expect attaching to it failing.

Markus Schaaf

unread,
Sep 30, 2009, 10:04:51 AM9/30/09
to
Thomas Steinbach wrote:

> I use FreeConsole() to finisch my WriteConsole operations, but
> that give not the console prompt back. How can I solve this?

I suppose cmd.exe is not aware of your 'stealing' of its console. Try if
that makes a difference:

start /wait yourapp

Thomas Steinbach

unread,
Sep 30, 2009, 1:29:24 PM9/30/09
to
Hello Markus,

uhhh, what do you mean with that "start/wait" ?

Have now this:

BOOL ShowCommandSyntax(HWND hWnd)
{
BOOL bCon = FALSE;
DWORD dwChW = 0;
HANDLE hStdOut = NULL;

bCon = AttachConsole(ATTACH_PARENT_PROCESS);
if( bCon ) {
/* console code */
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTitle( _T("Syntax:") );
WriteConsole(hStdOut, _T("Test"), _tcslen(_T("Test")), &dwChW, NULL);
FreeConsole();
}else {
/* gui code */
MessageBox(NULL, _T("Test"), _T("Syntax:"), MB_OK);
}
return TRUE;
}


perhaps useful for other... but one problem remains.
There is a blinking cursur and I have to press Return


to get the prompt back.

Thomas

Thomas Steinbach

unread,
Sep 30, 2009, 1:44:51 PM9/30/09
to
Hello Markus,

Ah, ok. You're right. Thank you. Didn't think about that.

If anybody ist interested
see my other posing for an example.
Message-ID: <ha04jf$apd$03$1...@news.t-online.com>


Thomas

0 new messages