简体   繁体   中英

How to know if a process handle is ready

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?

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 ie 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 ie the function has failed. Are you checking the return value for NULL? Have you used GetLastError() to see what's happening?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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