简体   繁体   English

如何在VC ++ 2005中使用OpenProcessToken,AdjustTokenPrivileges和GetExitCodeProcess

[英]How to use OpenProcessToken, AdjustTokenPrivileges, and GetExitCodeProcess in VC++ 2005

I read a couple of posts on how to check if a process has exited from a different process (I realize some people get hung up on semantics here, but just humor me) and I tried to implement it but am running into the error code 5 ("ERROR_ACCESS_DENIED") all over the place. 我读了几篇关于如何检查一个进程是否已退出不同进程的帖子(我发现有些人在这里挂起了语义,但只是幽默我)我尝试实现它但是遇到了错误代码5 (“ERROR_ACCESS_DENIED”)到处都是。

Here is what I do. 这就是我的工作。

1) Process 1 (P1) launches process 2 and writes to a shared memory location its own PID. 1)进程1(P1)启动进程2并将其自己的PID写入共享存储器位置。

2) Process 2 (P2) reads the PID from shared memory 2)进程2(P2)从共享内存中读取PID

3) P2 calls OpenProcess(...) with P1's PID to save a handle that it can check later. 3)P2使用P1的PID调用OpenProcess(...)以保存稍后可以检查的句柄。

4) P2 calls GetExitCodeProcess(...) with P1's PID repeatedly and checks for a STILL_ACTIVE code. 4)P2重复调用带有P1的PID的GetExitCodeProcess(...)并检查STILL_ACTIVE代码。

In the above method, I keep getting the ACCESS_DENIED error on GetExitCodeProcess. 在上面的方法中,我一直在GetExitCodeProcess上得到ACCESS_DENIED错误。 I've tried modifying P2's privileges by using the below code from MSDN's docs: 我尝试使用MSDN文档中的以下代码修改P2的权限:

HANDLE proc_h = OpenProcess(SYNCHRONIZE, FALSE, GetCurrentProcessId());
HANDLE hToken;
OpenProcessToken(proc_h, TOKEN_ADJUST_PRIVILEGES, &hToken);

LookupPrivilegeValue(NULL, lpszPrivilege, &luid );

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Enable the privilege
AdjustTokenPrivileges(hToken, 
                      FALSE, 
                      &tp, 
                      sizeof(TOKEN_PRIVILEGES), 
                      (PTOKEN_PRIVILEGES) NULL, 
                      (PDWORD) NULL);

But I keep getting the ACCESS_DENIED error on the call to OpenProcessToken(...) method. 但是我一直在调用OpenProcessToken(...)方法时遇到ACCESS_DENIED错误。 So does this indicate some sort of system level hurdle? 那么这是否表明某种系统级障碍? I do have admin rights on my machine and I'm running XP. 我的机器上有管理员权限,我正在运行XP。

Thanks in advance for any help. 在此先感谢您的帮助。

The handle passed to GetExitCodeProcess requires PROCESS_QUERY_INFORMATION access right. 传递给GetExitCodeProcess的句柄需要PROCESS_QUERY_INFORMATION访问权限。 The following works fine: 以下工作正常:

int main(int a_argc, char** a_argv)
{
    int pid = atoi(*(a_argv + 1));

    HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);

    if (NULL != h)
    {
        Sleep(2000);
        DWORD exit_code;
        if (FALSE == GetExitCodeProcess(h, &exit_code))
        {
            std::cerr << "GetExitCodeProcess() failure: " <<
                GetLastError() << "\n";
        }
        else if (STILL_ACTIVE == exit_code)
        {
            std::cout << "Still running\n";
        }
        else
        {
            std::cout << "exit code=" << exit_code << "\n";
        }
    }
    else
    {
        std::cerr << "OpenProcess() failure: " << GetLastError() << "\n";
    }

    return 0;
}

Instead of polling on GetExitCodeProcess open the handle with SYNCHRONIZE and wait for it to exit: 而不是轮询GetExitCodeProcess打开具有SYNCHRONIZE的句柄,并等待它退出:

int main(int a_argc, char** a_argv)
{
    int pid = atoi(*(a_argv + 1));

    HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pid);

    if (NULL != h)
    {
        WaitForSingleObject(h, 5000); // Change to 'INFINITE' wait if req'd
        DWORD exit_code;
        if (FALSE == GetExitCodeProcess(h, &exit_code))
        {
            std::cerr << "GetExitCodeProcess() failure: " <<
                GetLastError() << "\n";
        }
        else if (STILL_ACTIVE == exit_code)
        {
            std::cout << "Still running\n";
        }
        else
        {
            std::cout << "exit code=" << exit_code << "\n";
        }
    }
    else
    {
        std::cerr << "OpenProcess() failure: " << GetLastError() << "\n";
    }

    return 0;
}

OpenProcesstoken requires PROCESS_QUERY_INFORMATION you are opening the process with only SYNCHRONIZE access. OpenProcesstoken需要PROCESS_QUERY_INFORMATION才能打开只有SYNCHRONIZE访问权限的进程。 See if you add | PROCESS_QUERY_INFORMATION 看看你是否添加| PROCESS_QUERY_INFORMATION | PROCESS_QUERY_INFORMATION if it works. | PROCESS_QUERY_INFORMATION如果有效。

If you just want P2 to do something when P1 exits, there's another way that's probably rather easier: have P1 create a pipe and let P2 inherit a handle to that pipe. 如果你只是希望P2在P1退出时做某事,那么另一种方式可能相当容易:让P1创建一个管道并让P2继承该管道的句柄。 In P2, execute a read from the pipe. 在P2中,执行管道读取。 When P2's call to ReadFile returns with an error of ERROR_BROKEN_PIPE , P1 has exited. 当P2对ReadFile的调用返回错误ERROR_BROKEN_PIPE ,P1已退出。

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

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