等待指定线程结束

//该函数用于提升程序的权限,使之可以等待进程结束

bool CDSUMDlg::EnableDebugPrivilege()

{

 HANDLE hToken;

 bool bSucceed = false;

 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))

 {

  TOKEN_PRIVILEGES tp;

  tp.PrivilegeCount=1;

  if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid))

   return false;

  tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

  if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL))

   return false;

  CloseHandle(hToken);

 }

 return true;

}  

//此函数等待线程结束

bool CDSUMDlg::WaitAppClose(CArray* pArrayProcessID)

{

 int nProcessCount = 0, nResult = -1;

 HANDLE hProcess = NULL;

 CString sTmp;

 DWORD dwProcessID;

 nProcessCount = pArrayProcessID->GetCount();

 if (nProcessCount == 0)

  return true;

 if (!EnableDebugPrivilege())

 {

  return false;

 }

 for (int i = 0; i < nProcessCount; i++)

 {

  dwProcessID = pArrayProcessID->GetAt(i);

  hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwProcessID);

  if (hProcess)

  {

   if (dwProcessID == m_dwProcessID)

   {

   nResult = WaitForSingleObject(hProcess, WAITAPPCLOSE_TIMEOUT);

   if (nResult == -1)

   {

    sTmp.Format(L"Wait application close failed (%d).", GetLastError());

    return false;

   }

   else if (nResult == WAIT_TIMEOUT)

   {

    sTmp.Format(L"Wait application close time out.");

    return false;

   }

  }

  else

  {

   sTmp.Format(L"Wait close failed (%d).", GetLastError());

   return false;

  }

 }

 }

 return true;

}

 

你可能感兴趣的:(VC++)