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

GUI with Console Window and FreeConsole()

318 views
Skip to first unread message

Thomas Steinbach

unread,
Oct 10, 2009, 8:45:03 AM10/10/09
to
Hello,

I have a win32 (plain C/WinAPI) GUI program.
In the program I attach a console, if the programm is started
from the commandline and print out the command syntax to
that console, which is the parent process.

All seems to be fine, but after FreeConsole() the cursor
doesn't return to the command prompt and remains at the
end of the console output.

Only if I press return or CTRL+C in the console window,
the prompt returns.

Have the following code:

BOOL ShowConsoleOrGuiSyntax(HWND hWnd)
{
BOOL bCon = FALSE;
DWORD dwChW = 0;
HANDLE hStdOut = NULL;
//DWORD dwPidCon, dwPidCur;

bCon = AttachConsole(ATTACH_PARENT_PROCESS);
if( bCon ) {
/* console code */
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsole(hStdOut, _T("Usage: thisApp.exe -arg1"),
_tcslen(_T("Usage: thisApp.exe -arg1"")), &dwChW, NULL);
FreeConsole();

//dwPidCur = GetCurrentProcessId();
//dwPidCon = GetParentProcessPid(&dwPidCur); /* 1 */
//GenerateConsoleCtrlEvent(CTRL_C_EVENT, dwPidCon);

}else {
/* gui code */
MessageBox(NULL, _T("Usage: thisApp.exe -arg1""), _T("Syntax:"),
MB_OK);
}
return TRUE;
}

1) user defined funtion which returns parents pid

Does anybody know how to "really" release the console
of the parent process and get back the prompt without any user input?

I also tried to send an CTRL+C event to the Console
(see code in comment), but I got an "The handle is invalid"
(GetLastError = #6)

Hope that someone can help me.


Thomas

Alex Blekhman

unread,
Oct 10, 2009, 5:10:45 PM10/10/09
to
"Thomas Steinbach" wrote:
> I also tried to send an CTRL+C event to the Console (see code in
> comment), but I got an "The handle is invalid" (GetLastError =
> #6)

Have you tried to generate CTRL+C event before calling
FreeConsole? After FreeConsole returns your process cannot
reference the console anymore.

Alex

Thomas Steinbach

unread,
Oct 13, 2009, 10:17:51 AM10/13/09
to
Hello Alex,

Yes. If I generate CTRL+C before FreeConsole() I get the error
#87 = "The parameter is incorrect" and after FreeConsole() I get the
#6 = "The Handle is invalid" error

A published a full and complete (Visual Studio 2008 Express)
project with example code which demonstrates this behavior at:
http://www.failure.bravehost.com/prog/win32/consam/

Hope that somone can tell me what to do.

Thomas

0 new messages