我可以挂起一个线程以外的进程吗?

bTa*_*ger 2 c++ dll mfc system

我将挂起(或暂停)除一个线程之外的进程。

我尝试使用 SuspendThread(Api Function),结果是进程线程变成了不负责任的状态。

这不是我想要的。我想让简历成为我必须做的主要工作的一个线程。

我该如何解决这个问题?请给出你的想法。

谢谢。

Cap*_*ous 5

您可以调用CreateToolhelp32Snapshot以获取属于某个进程的线程列表。一旦您拥有该列表,只需遍历它并挂起每个与当前线程 ID 不匹配的线程。下面的示例未经测试,但应该可以正常工作。

#include <windows.h>
#include <tlhelp32.h>

// Pass 0 as the targetProcessId to suspend threads in the current process
void DoSuspendThread(DWORD targetProcessId, DWORD targetThreadId)
{
    HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    if (h != INVALID_HANDLE_VALUE)
    {
        THREADENTRY32 te;
        te.dwSize = sizeof(te);
        if (Thread32First(h, &te))
        {
            do
            {
                if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID)) 
                {
                    // Suspend all threads EXCEPT the one we want to keep running
                    if(te.th32ThreadID != targetThreadId && te.th32OwnerProcessID == targetProcessId)
                    {
                        HANDLE thread = ::OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
                        if(thread != NULL)
                        {
                            SuspendThread(thread);
                            CloseHandle(thread);
                        }
                    }
                }
                te.dwSize = sizeof(te);
            } while (Thread32Next(h, &te));
        }
        CloseHandle(h);    
    }
}
Run Code Online (Sandbox Code Playgroud)