Click here to Skip to main content
15,926,174 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to intercept Console Window Close Event Pin
david bagaturia14-Mar-07 0:08
david bagaturia14-Mar-07 0:08 
QuestionRe: How to intercept Console Window Close Event Pin
Programm3r14-Mar-07 0:18
Programm3r14-Mar-07 0:18 
AnswerRe: How to intercept Console Window Close Event Pin
david bagaturia14-Mar-07 0:41
david bagaturia14-Mar-07 0:41 
QuestionRe: How to intercept Console Window Close Event Pin
Programm3r14-Mar-07 0:49
Programm3r14-Mar-07 0:49 
Question[Message Deleted] Pin
kakan14-Mar-07 0:56
professionalkakan14-Mar-07 0:56 
AnswerRe: How to intercept Console Window Close Event Pin
Cedric Moonen14-Mar-07 1:08
Cedric Moonen14-Mar-07 1:08 
GeneralRe: How to intercept Console Window Close Event Pin
kakan14-Mar-07 1:11
professionalkakan14-Mar-07 1:11 
AnswerRe: How to intercept Console Window Close Event Pin
Stephen Hewitt14-Mar-07 3:47
Stephen Hewitt14-Mar-07 3:47 
This code leaks HANDLEs: the hProcess and hThread members of the PROCESS_INFORMATION structure need to be closed with CloseHandle if the call to CreateProcess succeeds (and you don't want to use them). Closing the HANDLEs will not close the process. Also your wait is a busy wait; this is a bad thing in a multitasking OS. You should use the WaitForSingleObject API instead. Try code like this:

// CreateAndWait.cpp : Defines the entry point for the application.
//
 
#include "stdafx.h"
 
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	STARTUPINFO si = {0};
	si.cb = sizeof(si);
	PROCESS_INFORMATION pi;
 
 	BOOL bOK = CreateProcess(
			"C:\\Windows\\Notepad.exe",
			NULL,
			NULL,
			NULL,
			FALSE,
			0,
			NULL,
			NULL,
			&si,
			&pi
			);
	if (bOK)
	{
		// We don't need the thread handle; close it or leak!
		CloseHandle(pi.hThread);
 
		// Wait for notepad to exit; not a busy wait!
		WaitForSingleObject(pi.hProcess, INFINITE);
 
		// Close the process handle now we're done with it or we leak!
		CloseHandle(pi.hProcess);
 
		MessageBox(NULL, "Notepad has closed", NULL, MB_OK);
	}
 
	return 0;
}


This will launch notepad (if you installed windows to "C:\") and wait for it to finish without spinning in a loop and wasting CPU cycles. It will not leak any HANDLEs.


Steve

AnswerRe: How to intercept Console Window Close Event Pin
Stephen Hewitt14-Mar-07 3:53
Stephen Hewitt14-Mar-07 3:53 
GeneralRe: How to intercept Console Window Close Event Pin
Programm3r15-Mar-07 19:49
Programm3r15-Mar-07 19:49 
AnswerRe: How to intercept Console Window Close Event Pin
prasad_som14-Mar-07 7:49
prasad_som14-Mar-07 7:49 
GeneralRe: How to intercept Console Window Close Event Pin
Stephen Hewitt14-Mar-07 12:54
Stephen Hewitt14-Mar-07 12:54 
GeneralRe: How to intercept Console Window Close Event Pin
prasad_som14-Mar-07 18:08
prasad_som14-Mar-07 18:08 
GeneralRe: How to intercept Console Window Close Event Pin
Programm3r15-Mar-07 19:49
Programm3r15-Mar-07 19:49 
GeneralRe: How to intercept Console Window Close Event Pin
prasad_som15-Mar-07 19:56
prasad_som15-Mar-07 19:56 
QuestionCursor help Pin
rushiraj.jhala13-Mar-07 23:28
rushiraj.jhala13-Mar-07 23:28 
AnswerRe: Cursor help Pin
Parthi_Appu14-Mar-07 0:36
Parthi_Appu14-Mar-07 0:36 
GeneralRe: Cursor help Pin
rushiraj.jhala14-Mar-07 0:58
rushiraj.jhala14-Mar-07 0:58 
GeneralRe: Cursor help Pin
_AnsHUMAN_ 14-Mar-07 1:10
_AnsHUMAN_ 14-Mar-07 1:10 
GeneralRe: Cursor help Pin
rushiraj.jhala14-Mar-07 1:56
rushiraj.jhala14-Mar-07 1:56 
AnswerRe: Cursor help Pin
Parthi_Appu14-Mar-07 2:30
Parthi_Appu14-Mar-07 2:30 
GeneralRe: Cursor help Pin
_AnsHUMAN_ 14-Mar-07 2:48
_AnsHUMAN_ 14-Mar-07 2:48 
GeneralRe: Cursor help Pin
Parthi_Appu14-Mar-07 3:01
Parthi_Appu14-Mar-07 3:01 
GeneralRe: Cursor help Pin
rushiraj.jhala14-Mar-07 2:52
rushiraj.jhala14-Mar-07 2:52 
QuestionProblem with Release version Pin
ilgale13-Mar-07 22:54
ilgale13-Mar-07 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.