如何知道进程句柄是否准备好

发布于 2024-12-08 10:03:59 字数 2265 浏览 0 评论 0原文

我在 C++ 中使用 OpenProcess 打开一个进程,但在获取它后我无法立即使用它,因为我收到“无效句柄错误”。我知道正确的句柄,因为当我在此句柄上执行 GetProcessId 时,它会为我提供正确的 PID。 这就是我打开流程的方式。

#include <windows.h>
#include <stdio.h>
#include <dbghelp.h>
#pragma (lib, "dbghelp.lib");

bool EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable) 
{ 
    HANDLE hToken; 
    TOKEN_PRIVILEGES    tp; 
    LUID luid; 
    bool ret; 

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY | TOKEN_READ, &hToken)) 
        return FALSE; 

    if (!LookupPrivilegeValue(NULL, lpszPrivilegeName, &luid)) 
        return FALSE; 

    tp.PrivilegeCount           = 1; 
    tp.Privileges[0].Luid       = luid; 
    tp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0; 

    ret = AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL); 
    CloseHandle(hToken); 

    return ret; 
}

void main()
{
    EnablePrivilege(SE_DEBUG_NAME, TRUE);

    STARTUPINFOA startInfo;
    PROCESS_INFORMATION processInfo;
    ZeroMemory( &startInfo, sizeof(startInfo) );
    startInfo.cb = sizeof(startInfo);
    ZeroMemory( &processInfo, sizeof(processInfo) );
    DWORD creationFlags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION;
    const char* comLine = "Some process path and name";

//     Start the child process. 
    if( CreateProcessA( NULL,   // No module name (use command line)
       (LPSTR)comLine, //argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        creationFlags,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &startInfo,            // Pointer to STARTUPINFO structure
        &processInfo )           // Pointer to PROCESS_INFORMATION structure
     == false ) 
    {
        printf("FAIL!");
return;
    }

    SetLastError(0);
    bool ok = SymInitialize(processInfo.hProcess, NULL, true);
    int err = GetLastError();

}

由于某种原因,最后一个错误值是垃圾。
有没有办法检查进程句柄是否可供使用?

I open a process with OpenProcess in c++ but I can't use it right after I get it because I get an "Invalid handle error". I know that the right handle because it gives me the right PID when I execute GetProcessId on this handle.
This is how I open the process.

#include <windows.h>
#include <stdio.h>
#include <dbghelp.h>
#pragma (lib, "dbghelp.lib");

bool EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable) 
{ 
    HANDLE hToken; 
    TOKEN_PRIVILEGES    tp; 
    LUID luid; 
    bool ret; 

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY | TOKEN_READ, &hToken)) 
        return FALSE; 

    if (!LookupPrivilegeValue(NULL, lpszPrivilegeName, &luid)) 
        return FALSE; 

    tp.PrivilegeCount           = 1; 
    tp.Privileges[0].Luid       = luid; 
    tp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0; 

    ret = AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL); 
    CloseHandle(hToken); 

    return ret; 
}

void main()
{
    EnablePrivilege(SE_DEBUG_NAME, TRUE);

    STARTUPINFOA startInfo;
    PROCESS_INFORMATION processInfo;
    ZeroMemory( &startInfo, sizeof(startInfo) );
    startInfo.cb = sizeof(startInfo);
    ZeroMemory( &processInfo, sizeof(processInfo) );
    DWORD creationFlags = DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION;
    const char* comLine = "Some process path and name";

//     Start the child process. 
    if( CreateProcessA( NULL,   // No module name (use command line)
       (LPSTR)comLine, //argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        creationFlags,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &startInfo,            // Pointer to STARTUPINFO structure
        &processInfo )           // Pointer to PROCESS_INFORMATION structure
     == false ) 
    {
        printf("FAIL!");
return;
    }

    SetLastError(0);
    bool ok = SymInitialize(processInfo.hProcess, NULL, true);
    int err = GetLastError();

}

For some reason the last erro value is garbage.
Is there a way to check if the process handle is ready for use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-12-15 10:03:59

从问题中函数的文档来看:

Return value

If the function succeeds, the return value is an open handle to the specified process.

这意味着如果函数成功,您已经获得了一个可用的句柄,即您要么获得它,要么不获得它,不存在“您必须等待”它可用的中间立场。在您的特定情况下,您的句柄很可能无效,即该功能失败。您是否检查返回值是否为 NULL?您是否使用过 GetLastError()< /a> 看看发生了什么?

From the documentation of the function in questions:

Return value

If the function succeeds, the return value is an open handle to the specified process.

This means that if the function succeeds you already get a usable handle i.e. you either get it or not, there is not middle ground where "you have to wait" for it to be usable. In your particular case chances are that your handle is invalid i.e the function has failed. Are you checking the return value for NULL? Have you used GetLastError() to see what's happening?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文