复习笔记之一 --暂停或者重启进程中的所有线程

来源:互联网 发布:微信试探老公 知乎 编辑:程序博客网 时间:2024/05/17 22:51

//****************************************************//

//输入参数:dwProcessId:进程ID。fSuspend:是否暂停。//

// GetCurrentProcessId()获得当前可执行程序进程的ID  返回 DWORD//

//****************************************************//

void SuspendProcess(DWORD dwProcessId,BOOL fSuspend)

{

    //Takes a snapshot of the specified processes, as well as the heaps, modules, and threads used by these processes.

    //?ì?°????¨?ì?¨??ê??¨1¤?§?¨?Dì??죤a°???线?¨?

    //TH32CS_SNAPTHREADêo????ì?线?¨?

    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,dwProcessId);

    if(hSnapshot != INVALID_HANDLE_VALUE)

    {

       //Describes an entry from a list of the threads executing in the system when a snapshot was taken.

       //THREADENTRY32êo¨¨o?°???ì?ì?¨|??D线?¨?¢Dà¨aì?¨?¨2

       /*  typedef struct tagTHREADENTRY32 {

       DWORD dwSize;

       DWORD cntUsage;

       DWORD th32ThreadID;

       DWORD th32OwnerProcessID;

       LONG  tpBasePri;

       LONG  tpDeltaPri;

       DWORD dwFlags;

       } THREADENTRY32, *PTHREADENTRY32;*/

       THREADENTRY32 te = {sizeof(te)};

       //Retrieves information about the first thread of any process encountered in a system snapshot.

       //Thread32Firstêo????ì?ì¨2°??¨?¨2ì?¨¢1??D

       BOOL fok = Thread32First(hSnapshot,&te);

       for (;fok;fok = Thread32Next(hSnapshot,&te))

       {

           if (te.th32OwnerProcessID == dwProcessId)

           {   //?¨°a线?¨?ê??ì?线?¨??à¨2

              HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME,FALSE,te.th32ThreadID);

              if (hThread != NULL)

              {

                  if (fSuspend)

                  {

                     SuspendThread(hThread);

                  }

                  else

                  {

                     ResumeThread(hThread);

                  }

              }

              CloseHandle(hThread);

           }

       }

    }

    CloseHandle(hSnapshot);

}

0 0
原创粉丝点击