windows - How can I wait for an application launched by another application launched by my application Qt/C++ -
i'm creating windows add/remove programs application in qt 5.4 , i'm becaming crazy solve little "puzzle":
my application (app_0) runs application (app_1) , waits app_1 until terminates.
app_1 uninstaller (i.e. uninstall.exe) , i've not source code of app_1, of qt app_0.
app_1, instead of doing uninstall job, copies somewhere in filesystem (i saw au_.exe other apps use different names , locations), runs copy of (app_2) , terminates.
the app_2 has gui , job i'm waiting (uninstall) demanded final user of running app_2.
in situation application (app_0) stops waiting app_1 pratically (because launches app_1 , waits app_1). work properly, obviously, need know instead when app_2 terminated...
so question is: there way (using techniques (hooking?)) know if , when app_2 terminates?
note: consider standard windows add/remove programs utility job (it seems waits app_2). can test this, example, installing adobe digital edition. uninstaller (uninstall.exe) copies new folder in user_local_temp folder au_.exe, runs , terminates. os utility waits au_.exe , after terminates refreshes list of installed programs.
if kind of technique (uninstall.exe copies somewhere same name (au_.exe) ) problem resolved, obviously, simply. don't think name of copied uninstaller same , don't assume things i'm not sure real.
many in advance
thanks iinspectable's suggestion (see comment... , many guy!) created function solves problems! i'll share here function useful other people same (or similar) problem.
for needs, function receives parameter index of item uninstalled (from qlist) , gets uninstall string (for example: c:\programfiles\myapp\uninstall.exe).
then uninstall string, i'll create process (createprocess) , put handle job object, function wait processes ran process.
the function pretty simple , can improved. notice process must created create_breakaway_from_job option, otherwise assignprocesstojobobject fail "access denied" error.
void mainwindow::unibuttonclick(int idx) { qmessagebox::standardbutton reply; qmessagebox::standardbutton err; reply = qmessagebox::question(this, "uninstall/change", "uninstall " + ip[idx].displayname +"?\r\n\r\n" + ip[idx].uninstallstring, qmessagebox::yes|qmessagebox::no); if (reply == qmessagebox::yes) { //qstring s = "c:\\windows\\notepad.exe"; // test job assignment , createprocess qstring s = ip[idx].uninstallstring; // real uninstaller string qstring jobname = "myjobobject"; try { process_information processinfo; //this [out] parameter startupinfo startupinfo; //this [in] parameter pjobobject_basic_process_id_list plist; handle hprocess; bool bjoballend; zeromemory(&startupinfo, sizeof(startupinfo)); startupinfo.cb = sizeof startupinfo ; //only compulsory field wchar_t* path; path = (wchar_t*) malloc (sizeof(wchar_t)*s.length()+1); s.towchararray(path); path[s.length()]=0; // null terminate string // create process create_breakaway_from_job overcome accessdenied issue on assignprocesstojobobject. if(createprocess(null, path, null, null, false, create_breakaway_from_job|create_suspended, null, null,&startupinfo, &processinfo)) { plist = (pjobobject_basic_process_id_list)globalalloc(gmem_fixed, 10000); handle jobobj = createjobobject(null, (const wchar_t*)jobname.utf16()); if (assignprocesstojobobject(jobobj, processinfo.hprocess) != 0) { resumethread(processinfo.hthread); // process assigned jobobjext, resume { queryinformationjobobject(jobobj, jobobjectbasicprocessidlist, plist, 10000, null); bjoballend = true; for(dword i=0; i<plist->numberofprocessidsinlist; i++) { hprocess = openprocess(synchronize, false, plist->processidlist[i]); if(hprocess != null) { closehandle(hprocess); bjoballend = false; } } sleep(500); } while(!bjoballend); } else qdebug() << "assignprocess job failed: error = " << qstring::number(getlasterror()); globalfree(plist); closehandle(jobobj); closehandle(processinfo.hthread); closehandle(processinfo.hprocess); } } catch(qstring error) { qmessagebox::critical(this, "file not found!", "the requested uninstaller doesn't exists", qmessagebox::ok); } // refresh list handlebutton(); } }
Comments
Post a Comment