Bug in waiting for process to end function

Source: Internet
Author: User

Accidentally found a bug, there is a function is written like this:


 void   Waitprocexit (DWORD dwpid) {HANDLE hprocess  = OpenProcess (process_all_access, 0      if  (NULL == hprocess) {DWORD Dwerr         = GetLastError (); Printf (  getlasterror=%d.\n  Span style= "color: #800000;"    > "  // do something after the target process        Exit  //  ....  return  ;}  



The function is to wait for the incoming PID process to exit and then perform some business.
However, in some environments execution is not expected.

By GetLastError The result is 5, which is the permission problem.
OpenProcess failed because the process in which the Dwpid is located is Administrator rights, and the process that executes the waitprocexit () function is a user right.
The change code should be modified to:

void waitprocexit (DWORD dwpid) {    0, dwpid);     if (NULL = = hprocess)    {        = GetLastError ();        Printf ("getlasterror=%d.\n", Dwerr);    }    WaitForSingleObject (hprocess,infinite);         // Do something after the target process exit     // ....        return ;}



The Synchronize permission openprocess should be used.
The SYNCHRONIZE description is: The right and the object for synchronization. This enables a thread to wait until the object was in the signaled state.

See also: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880%28v=vs.85%29.aspx

Bug in waiting for process to end function

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.