获取进程句柄

HANDLE GetProcessHandle(const char* sProcessName)
{
    //获取进程快照
    HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapShot == INVALID_HANDLE_VALUE)
    {
        //AfxMessageBox("未能获取到进程快照");
        return NULL;
    }
    PROCESSENTRY32  thePE;
    thePE.dwSize = sizeof(PROCESSENTRY32);

    //遍历正在运行的第一个系统进程
    int     nResult = 0;
    HANDLE hProcess = NULL;
    if (Process32First(hSnapShot, &thePE))
    {
        do
        {
            //找到对应进程
            if (!strcmp(thePE.szExeFile, sProcessName))
            {
                //结束指定的进程
                hProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE,
                                        false, thePE.th32ProcessID);
                break;
            }
        } while (Process32Next(hSnapShot, &thePE));
    }

    CloseHandle(hSnapShot);
    return hProcess;
}

猜你喜欢

转载自blog.csdn.net/t60339/article/details/82012184
今日推荐