Windows core programming 5 jobs

Source: Internet
Author: User

Run the code first.

You can ask questions if you don't know!

# Include <iostream> # include <vector> # include <map> # include <windows. h> # include <tchar. h> # include <process. h> # include <strsafe. h> void createprocessinjob (ptstr scommangline, handle hjob) {startupinfo stinfo = {sizeof (stinfo)}; process_information process_info = {0}; int Len = _ tcsclen (scommangline) * sizeof (tchar); int lsize = Len + sizeof (tchar); ptstr pbuffer = new tchar [lsize]; memset (pbuffer, 0, lsize );/ /Size must contain the null character stringcbcopy (pbuffer, lsize, scommangline); // The buffer of parameter 2 must be writable, And the access violation bool Bok = CreateProcess (null, pbuffer, null, false, create_breakaway_from_job | create_suincluded, null, null, & stinfo, & process_info) ;__ try {If (! Bok) _ leave; assignprocesstojobobject (hjob, process_info.hprocess); // activation thread: resumethread (process_info.hthread) ;:__ finally {Delete [] pbuffer; pbuffer = NULL; // closes the process object. The thread object closehandle (process_info.hprocess); closehandle (process_info.hthread) ;}} unsigned int winapi threadfunc (lpvoid lparam) {handle hiocp =; bool bgoon = true; DWORD dwlen = 0; DWORD dwprocessid; ulong_ptr comkey; lpoverlapped ov; whil E (bgoon & getqueuedcompletionstatus (hiocp, & dwlen, & comkey, & ov, infinite) {If (comkey = 1) {Switch (dwlen) {Case job_object_msg_exit_process: dwprocessid = (DWORD) ov; printf ("process ID % d \ n", dwprocessid); break; Case job_object_msg_active_process_zero: printf ("over all"); ov; bgoon = false; break; Case job_object_msg_new_process: printf ("new process ID % d \ n", (DWORD) OV); break ;}}return 0;} int main () {// get th E List of process identifiers. handle hjob = NULL; handle hioport = NULL; handle hthread = NULL; ulong_ptr completionkey = 1; // User-Defined _ Try {hjob = createjobobject (null, text ("myjob"); If (null = hjob) {__ leave;} jobobject_basic_ui_restrictions taguirestrictions; taguirestrictions. uirestrictionsclass = job_object_uilimit_none; taguirestrictions. uirestrictionsclass = job_object_uilimit_handles | job_obje Ct_uilimit_exitwindows; bool Bok = setinformationjobobject (hjob, jobobjectbasicuirestrictions, & taguirestrictions, sizeof (taguirestrictions); If (! Bok) {__ leave;} hioport = createiocompletionport (invalid_handle_value, null, completionkey, 0); If (hioport = NULL) {__ leave;} define tagasscompletionport; tagasscompletionport. completionkey = (pvoid) completionkey; tagasscompletionport. completionport = hioport; Bok = setinformationjobobject (hjob, jobobjectassociatecompletionportinformation, & tagasscompletionport, sizeof (tagasscompl Etionport); If (! Bok) {__ leave;} hthread = (handle) _ beginthreadex (null, 0, threadfunc, hioport, 0, null); If (hthread = NULL) {__ leave ;} createprocessinjob (text ("C: \ Program Files \ Microsoft Visual Studio 8 \ common7 \ tools \ spyxx.exe"), hjob); createprocessinjob (text ("C: \ Users \ Administrator \ Desktop \ procexp.exe "), hjob); waitforsingleobject (hthread, infinite) ;__ finally {closehandle (hjob); closehandle (hioport ); closehandle (hthread); hioport = hthread = NULL;} system ("pause"); Return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.