简体   繁体   English

从不同进程中的另一个线程暂停/恢复一个线程或进程

[英]Suspend/Resume a thread or process from another Thread in different process

In one of my projects I create multiple processes (which are sub projects that I developed) from my main project as CREATE_SUSPENDED and storing their handles in a global array but;在我的一个项目中,我从我的主项目创建了多个进程(我开发的子项目)作为CREATE_SUSPENDED并将它们的句柄存储在一个全局数组中,但是; when I want to resume it with ResumeThread function I get ERROR_INVALID_HANDLE error code.当我想用ResumeThread函数恢复它时,我得到ERROR_INVALID_HANDLE错误代码。

The MSDN specifies that the thread must have THREAD_SUSPEND_RESUME access right and I couldn't find how to set it. MSDN指定线程必须具有 THREAD_SUSPEND_RESUME 访问权限,但我找不到如何设置它。

Do those handles are only valid on its process space and how can I suspend/resume a thread or process from another process with all the handles I stored?这些句柄是否仅在其进程空间中有效,我如何使用我存储的所有句柄从另一个进程挂起/恢复线程或进程?

Note: I know synchronization objects may have a use here but is it the only and only way?注意:我知道同步对象在这里可能有用,但这是唯一的方法吗?

One more issue is;还有一个问题是; how to obtain handle of a thread created by my ProcessClass from the main process, which also created that ProcessClass?如何从主进程中获取由我的 ProcessClass 创建的线程的句柄,该线程也创建了该 ProcessClass?

Here is a portion of the code这是代码的一部分

ProcessClass进程类

void ProcessClass::Start(){       
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&pi, sizeof(pi));
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);

    BOOL createdProc = CreateProcess(NULL, 
        ProcessExeDir, 
        NULL, 
        NULL, 
        TRUE, 
        CREATE_NEW_CONSOLE | CREATE_SUSPENDED
        , NULL, 
        NULL, 
        &si, 
        &pi); 
    if (createdProc == FALSE)
    {
        UINT32 errorCode = GetLastError();        
    }
    else
    {
        DWORD affinity;
        affinity = 0x000000010 << (5);
        SetProcessAffinityMask(pi.hProcess, affinity);

        ProcessHandler = pi.hProcess;
        ProcessInfoPtr->ProcHandle = ProcessHandler;

        // I can need this handle
        // CloseHandle(pi.hThread);
    }
}

// ProcessInPtr is defines as ProcessInfoStc* in the header
ProcessInfoStc* ProcessClass::GetProcessInfoPtr() {
    return ProcessInfoPtr;
}

In the Main Project:在主项目中:

int main() {
   // this is declared as ManageProcesses(const ProcessInfoStcPtr& processes)
    ManageProcesses(ProcessInfoArray);
    return 0;
}

In ManageProcess function which is in the same lib project with the ProcessClass and following structure definitions:在与 ProcessClass 和以下结构定义位于同一库项目中的 ManageProcess 函数中:

ProcessClass processObj = new ProcessClass();
processObj->Start();
...
HANDLE pHandle = processObj->GetProcessInfoPtr()->ProcHandle;
//! pHandle is is same with the created process handle as I debug
ResumeThread(pHandle); 
DWORD error = GetLastError(); // RETURNS INVALID HANDLE

And the Structure to store process is also in main process并且存储过程的结构也在主过程中

ProcessInfoStc {
    HANDLE ProcHandle ,
    DWORD ProcId..
 }
 ProcessInfoStc ProcessInfoArray[10] = {
     {
        0,0...
     },...
 }
 typedef ProcessInfoStc* ProcessInfoStcPtr;

In one of my projects I create multiple processes (which are sub projects that I developed) from my main project as CREATE_SUSPENDED and storing their handles in a global array but;在我的一个项目中,我从我的主项目创建了多个进程(我开发的子项目)作为CREATE_SUSPENDED并将它们的句柄存储在一个全局数组中,但是; when I want to resume it with ResumeThread function I get ERROR_INVALID_HANDLE error code.当我想用ResumeThread函数恢复它时,我得到ERROR_INVALID_HANDLE错误代码。

That is because you are passing a process handle ( pi.hProcess ) to ResumeThread() .那是因为您将进程句柄 ( pi.hProcess ) 传递给ResumeThread() You cannot resume a process using ResumeThread() .您不能使用ResumeThread()恢复进程 As its name implies, you need to pass it a thread handle instead ( pi.hThread ).顾名思义,您需要向它传递一个线程句柄( pi.hThread )。

The MSDN specifies that the thread must have THREAD_SUSPEND_RESUME access right and I couldn't find how to set it. MSDN 指定线程必须具有THREAD_SUSPEND_RESUME访问权限,但我找不到如何设置它。

You already have it, per the documentation:根据文档,您已经拥有它:

Creating Processes 创建流程

The thread and process handles are created with full access rights , although access can be restricted if you specify security descriptors.线程和进程句柄是使用完全访问权限创建的,但如果您指定安全描述符,则可以限制访问。

how to obtain handle of a thread created by my ProcessClass from the main process, which also created that ProcessClass ?如何从主进程中获取由我的ProcessClass创建的线程的句柄,该线程也创建了该ProcessClass

You are not currently storing the thread handle anywhere, you do can't retrieve it.您当前没有将线程句柄存储在任何地方,您确实无法检索它。 Update your ProcessInfoStc struct to store both HANDLE s that CreateProcess() returns.更新您的ProcessInfoStc结构以存储CreateProcess()返回的两个HANDLE And don't forget to close both of them when you are done using them.并且在使用完它们后不要忘记关闭它们。

I would recommend against stopping/resuming threads using the OS or threading library low level APIs, unless you are writing a thread scheduling algorithm (but then your code should reside in the kernel).我建议不要使用操作系统或线程库低级 API 来停止/恢复线程,除非您正在编写线程调度算法(但您的代码应该驻留在内核中)。 In a modern OS and correctly written multi-threaded application threads in a waiting state consume almost no resources, so there should really be no reason to intervene there.在现代操作系统和正确编写的多线程应用程序线程中,处于等待状态的线程几乎不消耗资源,因此真的没有理由进行干预。 That would be the reason why you will unlikely see thread.suspend() and thread.resume() in practical code examples nowdays.这就是为什么您现在不太可能在实际代码示例中看到thread.suspend()thread.resume()的原因。

I would just build an application the way that threads would be polling (via an abstraction of IOLoop, Dispatcher, Executor, Actor, etc.) on IO selector(s), task queue(s), and only wake up and do something when there's a work to be done.我只会以线程在 IO 选择器、任务队列上轮询(通过 IOLoop、Dispatcher、Executor、Actor 等的抽象)的方式构建一个应用程序,并且只在有一项工作要做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM