The following two functions can be used to start a process and obtain the main window handle.
Bool callback cdsupdate: enumdsumwindowsproc (hwnd, lparam)
{
DWORD dwid;
Enumparam * Pep = (enumparam *) lparam;
Getwindowthreadprocessid (hwnd, & dwid );
If (dwid = pep-> m_dwprocessid)
{
Pep-> m_hmainwnd = hwnd;
Return false;
}
Return true;
}
Hwnd cdsupdate: rundsum (cstring sdsumfile)
{
Enumparam EP;
Startupinfo Si;
Process_information PI;
Cstring sfolder;
Zeromemory (& Si, sizeof (startupinfo ));
Si. cb = sizeof (startupinfo );
Si. wshowwindow = sw_show;
Zeromemory (& Pi, sizeof (process_information ));
Updatelog (: Message, l "(rundsum): Start dsum .");
If (! CreateProcess (null, (lptstr) (lpctstr) sdsumfile, null, null, false, 0, null, sdsumfile. left (sdsumfile. reversefind ('//'), & Si, & PI ))
{
Updatelog (: error, l "(rundsum): Start dsum fail (% d).", getlasterror ());
Return NULL;
}
// Close process and thread handles.
Closehandle (PI. hprocess );
Closehandle (PI. hthread );
Ep. m_dwprocessid = pi. dwprocessid;
Ep. m_hmainwnd = NULL;
For (INT I = 0; I <50; I ++) // retry about 10 s.
{
Sleep (200 );
Enumwindows (enumdsumwindowsproc, (lparam) & EP );
If (Ep. m_hmainwnd)
Break;
}
If (! Ep. m_hmainwnd)
{
Updatelog (: error, l "(rundsum): couldn't find the hwnd of dsum .");
Return NULL;
}
Return ep. m_hmainwnd;
}