I use MFC Find console window and send key to console like this.

Code:
HWND w;

	w = FindWindow(NULL, L"c:\\users\\pkru\\documents\\visual studio 2010\\Projects\\CleanVirus\\Debug\\CleanVirus.exe");
			

			if(w==NULL)
				MessageBox(NULL,_T("Not Found"), _T("OK"), MB_OK);



			else{
				DWORD dwProcessId = 0;
GetWindowThreadProcessId( ::GetDesktopWindow(),
                          &dwProcessId );

	while(TRUE){

		//if(w!=NULL){
				
		//MessageBox(NULL,_T("Found"), _T("OK"), MB_OK);
	
	ir[0].EventType = KEY_EVENT;
    ir[0].Event.KeyEvent.bKeyDown = TRUE;
    ir[0].Event.KeyEvent.dwControlKeyState = 0;
    ir[0].Event.KeyEvent.uChar.UnicodeChar = 'y';
    ir[0].Event.KeyEvent.wRepeatCount = 1;
    ir[0].Event.KeyEvent.wVirtualKeyCode = 'Y';
    ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('Y', MAPVK_VK_TO_VSC);

    ir[1].EventType = KEY_EVENT;
    ir[1].Event.KeyEvent.bKeyDown = TRUE;
    ir[1].Event.KeyEvent.dwControlKeyState = 0;
    ir[1].Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
    ir[1].Event.KeyEvent.wRepeatCount = 1;
    ir[1].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
    ir[1].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);


    ir[2].EventType = KEY_EVENT;
    ir[2].Event.KeyEvent.bKeyDown = FALSE;
    ir[2].Event.KeyEvent.dwControlKeyState = 0;
    ir[2].Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
    ir[2].Event.KeyEvent.wRepeatCount = 1;
    ir[2].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
    ir[2].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);

	 dwTmp = 0;
    WriteConsoleInput(hConIn, ir, 3, &dwTmp);

	
	 Sleep( 1000 );
	}
	
	FreeConsole();
}
After program process success in console it not close console windows but it show like this in Output
The thread 'Win32 Thread' (0x1300) has exited with code 0 (0x0).
I think if I can check when VC will show this line, then I can use FreeConsole(); for close console windows.