CreateProcess and WaitForInputIdle
CString strFileName = "C:\\WINDOWS\\system32\\notepad.exe";
HWND hWnd;
STARTUPINFO si;
GetStartupInfo(&si);
PROCESS_INFORMATION pi;
int iResult = CreateProcess(strFileName,NULL,NULL,NULL,
FALSE, CREATE_NEW_CONSOLE,
NULL,NULL,&si,&pi);
if ( iResult == 0 )
{
AfxMessageBox("Fail to create the SubProcess");
return;
}
if ( WaitForInputIdle(pi.hProcess,10000) == 0 )
//if ( WaitForInputIdle(pi.hProcess,10000) == WAIT_TIMEOUT )
{
hWnd = ::FindWindow("Notepad","NULL");
//hWnd = ::FindWindow("NULL","Untitled - Notepad");
//hWnd = ::FindWindow("MSBLWindowClass","MSN Messenger");
if ( hWnd == NULL )
{
AfxMessageBox("Can NOT get the HANDLE of the Sub-Process!!!");
return;
}
CRect rect;
GetClientRect(rect);
::SetParent(hWnd,this->m_hWnd);
::MoveWindow(hWnd,rect.left,rect.top,rect.right,rect.bottom,TRUE);
}
为什么总是hWnd == NULL那?
是不是因为notepad打开速度太快?我换成msn messager还是无法得到hwnd.