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
> 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 );
> 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.
>> [...]
>> 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
>> [...]
>> 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
> 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.
> 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
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
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