windows 为什么 GetCurrentProcess 返回 -1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5818173/
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-09-15 16:44:30  来源:igfitidea点击:

Why does GetCurrentProcess return -1?

c++cwindowswinapi

提问by Kmd

In this small program, why does GetCurrentProcess()return -1?

在这个小程序中,为什么GetCurrentProcess()返回-1?

int _tmain(int argc, _TCHAR* argv[]) {
    HANDLE h = GetCurrentProcess(); // ret -1 
    printf("0x%x\n",(DWORD)h); 
    return 0;
}

What's wrong?

怎么了?

In Kernel32.GetCurrentProcessI see this:

Kernel32.GetCurrentProcess我看到这个:

OR EAX,FFFFFFFF  ; EAX - ?
RETN

回答by Skizz

That is correct, see this API reference for GetCurrentProcess.

没错,请参阅GetCurrentProcess 的此 API 参考

The GetCurrentProcessfunction retrieves a pseudo-handle for the current process, which is currently defined as (HANDLE)-1. However, because you should not assume that the value will never change, the GetCurrentProcessfunction is provided as an alternative to hard-coding the constant into your code.

GetCurrentProcess函数检索当前进程的伪句柄,当前定义为(HANDLE)-1。但是,因为您不应该假设该值永远不会改变,所以GetCurrentProcess提供该函数作为将常量硬编码到您的代码中的替代方法。

回答by user703016

-1is the pseudo-handle that represents the current process. It's normal.

-1是代表当前进程的伪句柄。这是正常的。

回答by vrajs5

There is nothing wrong with -1 value. It will indicate current process handle. You can refer remark section in this msdn page

-1 值没有任何问题。它将指示当前进程句柄。您可以参考此 msdn 页面中的备注部分