c++ - Get all processes opened in a certain Desktop -
i'm working on application creates new desktop when launched , using key combo can move , forth between original , new desktop. @ creation time, in new desktop new explorer.exe process started, user can start whatever applications desires.
when key combo sends exit command detected, new desktop closed, , return original one, applications user started in new desktop still running.
is there way handle on of processes opened in new desktop, having handle window station , hdesk handle new desktop?
thanks david heffernan's idea, able find following solution. having hdesk handle desktop, compare using getthreaddesktop function every thread system. i'm not sure it's performant solution, i'm open towards suggestions improvements, works fine:
int main(void) { // desktop handles hdesk currentdesktop = getseconddesktop(getcurrentthreadid()); hdesk seconddesktop = createdesktop(l"seconddesktop", null, null, 0, generic_all, null); // start processes in seconddesktop ... // process enumeration dword aprocesses[1024], cbneeded, cprocesses; unsigned int i; enumprocesses(aprocesses, sizeof(aprocesses), &cbneeded); cprocesses = cbneeded / sizeof(dword); (i = 0; < cprocesses; i++) { if (aprocesses[i] != 0) { dword pthreadid = listprocessthreads(aprocesses[i]); if (getseconddesktop(pthreadid) == seconddesktop) { terminateprocess(aprocesses[i]); } } } return 0; } dword listprocessthreads(dword dwownerpid) { handle hthreadsnap = invalid_handle_value; threadentry32 te32; // take snapshot of running threads hthreadsnap = createtoolhelp32snapshot(th32cs_snapthread, 0); if (hthreadsnap == invalid_handle_value) return(false); // fill in size of structure before using it. te32.dwsize = sizeof(threadentry32); // retrieve information first thread, // , exit if unsuccessful if (!thread32first(hthreadsnap, &te32)) { closehandle(hthreadsnap); // must clean snapshot object! return(false); } // walk thread list of system, // , display information each thread // associated specified process { if (te32.th32ownerprocessid == dwownerpid) { return te32.th32threadid; } } while (thread32next(hthreadsnap, &te32)); // don't forget clean snapshot object. closehandle(hthreadsnap); return 0; } bool terminateprocess(dword dwprocessid) { dword dwdesiredaccess = process_terminate; bool binherithandle = false; handle hprocess = openprocess(dwdesiredaccess, binherithandle, dwprocessid); if (hprocess == null) return false; uint uexitcode = 0; bool result = terminateprocess(hprocess, uexitcode); closehandle(hprocess); return result; }
Comments
Post a Comment