C++ 如何从进程ID获取进程句柄?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2221103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:36:47  来源:igfitidea点击:

how to get process handle from process id?

c++winapiwindows-vista

提问by anand

I have process Id , I want to get its process handle.

我有进程 Id ,我想得到它的进程句柄。

Is there any API available for that.

是否有任何可用的 API。

I tried to use OpenProcess but it returns NULL, and GetLastError =0.

我尝试使用 OpenProcess,但它返回 NULL,并且 GetLastError =0。

This I am trying on Vista.

这是我在 Vista 上尝试的。

I guess I need to enable SeDebugPrivilege before using OpenProcess . But for enabling SeDebugPrivilege I need to get its Process handle.

我想我需要在使用 OpenProcess 之前启用 SeDebugPrivilege 。但是为了启用 SeDebugPrivilege,我需要获取它的 Process 句柄。

回答by Matt Joiner

OpenProcess(PROCESS_ALL_ACCESS, TRUE, procId);

You'll need to verify that you're using a valid process ID, and that you're permitted the access rights you request from the process.

您需要验证您使用的是有效的进程 ID,并且您被允许从该进程请求访问权限。

回答by bdd

Is this what you are looking for?

这是你想要的?

HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
CloseHandle(processHandle); 

Also, here is some code I use to set debug privledge before injecting DLLs.

另外,这里有一些代码我用来在注入 DLL 之前设置调试权限。

void Loader::EnableDebugPriv(void)
{
    HANDLE              hToken;
    LUID                SeDebugNameValue;
    TOKEN_PRIVILEGES    TokenPrivileges;

    if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    {
        if(LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &SeDebugNameValue))
        {
            TokenPrivileges.PrivilegeCount              = 1;
            TokenPrivileges.Privileges[0].Luid          = SeDebugNameValue;
            TokenPrivileges.Privileges[0].Attributes    = SE_PRIVILEGE_ENABLED;

            if(AdjustTokenPrivileges(hToken, FALSE, &TokenPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
            {
                CloseHandle(hToken);
            }
            else
            {
                CloseHandle(hToken);
                throw std::exception("Couldn't adjust token privileges!");              
            }
        }
        else
        {
            CloseHandle(hToken);
            throw std::exception("Couldn't look up privilege value!");
        }
    }
    else
    {
        throw std::exception("Couldn't open process token!");
    }
}

I've used the above code on Windows Vista with success.

我在 Windows Vista 上成功使用了上面的代码。

回答by Igor

You would need elevated privileges. Also look at similar question here.

您需要提升权限。也看看这里的类似问题。

回答by trgs

I just had the exact same issue as described: OpenProcess() == NULL and GetLastError() == 0. Turned out to be the Common Language RunTime Support setting, was set to "Pure" should have been just "Common". Took me ages to find.

我只是遇到了与描述完全相同的问题:OpenProcess() == NULL 和 GetLastError() == 0。原来是公共语言运行时支持设置,设置为“纯”应该只是“通用”。我花了很长时间才找到。

For VS2010 c++ goto -> Project Properties -> Configuration Properties -> C/C++ -> General

对于VS2010 c++ goto -> Project Properties -> Configuration Properties -> C/C++ -> General

回答by herodot

If you have a process identifier, you can get the process handle by calling the OpenProcessfunction. OpenProcessenables you to specify the handle's access rights and whether it can be inherited.

如果您有进程标识符,则可以通过调用OpenProcess函数来获取进程句柄。OpenProcess使您能够指定句柄的访问权限以及它是否可以被继承。

FYI:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684868(v=vs.85).aspx

仅供参考:http: //msdn.microsoft.com/en-us/library/windows/desktop/ms684868(v=vs.85)
.aspx