For Programmers: Free Programming Magazines  


Home > Archive > VC Language > January 2006 > Problem with termination of a console app within an MFC app while console is working









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with termination of a console app within an MFC app while console is working
Bulent

2006-01-23, 7:08 pm

Hi,

I have a mfc application. It executes a console application with
CreateProcess which is a lenghtly process. I will hide the console app
from the user.

My problem is this: I want to close MFC app AND console application
While hidden console app. is still active. How can I do this?

Code is below:
void CCPSDoc::OnAnalyseRun()
{
// TODO: Add your command handler code here
CString str ;

str = "cmd /c waitdlg.exe" ; //an app can work 15 minutes

STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);



si.dwFlags = STARTF_USESHOWWINDOW ;
si.wShowWindow = SW_HIDE ;

ZeroMemory( &pi, sizeof(pi) );
CWaitCursor wait ;
//// Start the child process.

if( ! CreateProcess( NULL, // No module name (use command line).
(LPSTR)(LPCSTR) str ,// Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
AfxMessageBox( "CreateProcess failed." );
}


//WaitForSingleObject( pi.hProcess, INFINITE )
wait.Restore() ;

////Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

}

Vipin [MVP]

2006-01-23, 7:08 pm

your line contradicts:-
"I want to close MFC app AND console application
While hidden console app. is still active. "

You say you want to close the console when it is active!!!

Anyway I am going off now for the day, so someone else could help if you put
it properly.
--
Vipin Aravind

"Bulent" <hatipoglu.bulent@gmail.com> wrote in message
news:1138049529.796773.86460@g14g2000cwa.googlegroups.com...
> Hi,
>
> I have a mfc application. It executes a console application with
> CreateProcess which is a lenghtly process. I will hide the console app
> from the user.
>
> My problem is this: I want to close MFC app AND console application
> While hidden console app. is still active. How can I do this?
>
> Code is below:
> void CCPSDoc::OnAnalyseRun()
> {
> // TODO: Add your command handler code here
> CString str ;
>
> str = "cmd /c waitdlg.exe" ; //an app can work 15 minutes
>
> STARTUPINFO si;
> PROCESS_INFORMATION pi;
> ZeroMemory( &si, sizeof(si) );
> si.cb = sizeof(si);
>
>
>
> si.dwFlags = STARTF_USESHOWWINDOW ;
> si.wShowWindow = SW_HIDE ;
>
> ZeroMemory( &pi, sizeof(pi) );
> CWaitCursor wait ;
> //// Start the child process.
>
> if( ! CreateProcess( NULL, // No module name (use command line).
> (LPSTR)(LPCSTR) str ,// Command line.
> NULL, // Process handle not inheritable.
> NULL, // Thread handle not inheritable.
> FALSE, // Set handle inheritance to FALSE.
> 0, // No creation flags.
> NULL, // Use parent's environment block.
> NULL, // Use parent's starting directory.
> &si, // Pointer to STARTUPINFO structure.
> &pi ) // Pointer to PROCESS_INFORMATION structure.
> )
> {
> AfxMessageBox( "CreateProcess failed." );
> }
>
>
> //WaitForSingleObject( pi.hProcess, INFINITE )
> wait.Restore() ;
>
> ////Close process and thread handles.
> CloseHandle( pi.hProcess );
> CloseHandle( pi.hThread );
>
> }
>



Alex Blekhman

2006-01-24, 4:00 am

Jochen Kalmbach [MVP] wrote:
>
> This is a little bit complicated...
> In general there is not way to "correctly" close a
> console application, expect that you also share the same
> console. Then you can call "GenerateConsoleCtrlEvent".
> You could try "AttachConsole" to attach to the console of
> this process. Then you can call
> "GenerateConsoleCtrlEvent". Then a "good" application
> will terminate (but not all applications are "good" ;-( )
> The second way is to simply terminate the process with
> "TerminateProcess". This is normally a bad idea, because
> it might leave some resources/files left on the system...


And the third way is to use Job object and put newly created
processes there. Then, you can be sure that grandchildren
will be terminated, too.


Alex Blekhman

2006-01-24, 4:00 am

Jochen Kalmbach [MVP] wrote:
>
> Yes, I forgot...
> But it hase the same effect as callung
> "TerminateProcess"... whcih should be avoided, shouldn´t
> it?


Agreed. Process termination should be the last resort.




Ben Voigt

2006-01-26, 7:08 pm

"Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in message
news:O36ywDGIGHA.2704@TK2MSFTNGP15.phx.gbl...
> Hi Bulent!
>
>
> This is a little bit complicated...
> In general there is not way to "correctly" close a console application,
> expect that you also share the same console. Then you can call

*except*

Couldn't you post WM_CLOSE to the console window? Although I see that
hiding was accomplished without a handle so this might be tricky.


> "GenerateConsoleCtrlEvent". You could try "AttachConsole" to attach to the
> console of this process. Then you can call "GenerateConsoleCtrlEvent".
> Then a "good" application will terminate (but not all applications are
> "good" ;-( )
>
> The second way is to simply terminate the process with "TerminateProcess".
> This is normally a bad idea, because it might leave some resources/files
> left on the system...
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/



Joseph M. Newcomer

2006-01-30, 7:07 pm

Generally, doing a WaitForSingleObject on the child process is a Really Bad Idea, because
it locks up your GUI thread.

You will need to signal your console app. Look at SetConsoleCtrlHandler and
GenerateConsoleCtrlEvent to notify the child process that it should terminate. Also use
the CREATE_NEW_PROCESS_GROUP flag for CreateProcess, as described in
GenerateConsoleCtrlEvent.
joe

On 23 Jan 2006 12:52:09 -0800, "Bulent" <hatipoglu.bulent@gmail.com> wrote:

>Hi,
>
>I have a mfc application. It executes a console application with
>CreateProcess which is a lenghtly process. I will hide the console app
>from the user.
>
>My problem is this: I want to close MFC app AND console application
>While hidden console app. is still active. How can I do this?
>
>Code is below:
>void CCPSDoc::OnAnalyseRun()
>{
> // TODO: Add your command handler code here
> CString str ;
>
> str = "cmd /c waitdlg.exe" ; //an app can work 15 minutes
>
> STARTUPINFO si;
> PROCESS_INFORMATION pi;
> ZeroMemory( &si, sizeof(si) );
> si.cb = sizeof(si);
>
>
>
> si.dwFlags = STARTF_USESHOWWINDOW ;
> si.wShowWindow = SW_HIDE ;
>
> ZeroMemory( &pi, sizeof(pi) );
> CWaitCursor wait ;
> //// Start the child process.
>
> if( ! CreateProcess( NULL, // No module name (use command line).
> (LPSTR)(LPCSTR) str ,// Command line.
> NULL, // Process handle not inheritable.
> NULL, // Thread handle not inheritable.
> FALSE, // Set handle inheritance to FALSE.
> 0, // No creation flags.
> NULL, // Use parent's environment block.
> NULL, // Use parent's starting directory.
> &si, // Pointer to STARTUPINFO structure.
> &pi ) // Pointer to PROCESS_INFORMATION structure.
> )
> {
> AfxMessageBox( "CreateProcess failed." );
> }
>
>
> //WaitForSingleObject( pi.hProcess, INFINITE )
> wait.Restore() ;
>
> ////Close process and thread handles.
> CloseHandle( pi.hProcess );
> CloseHandle( pi.hThread );
>
>}

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com