简体   繁体   English

AttachConsole错误5:访问被拒绝

[英]AttachConsole Error 5: Access is denied

I'm working with a C++ Console Application in Visual Studio 2013, working on Windows. 我正在使用Visual Studio 2013中的C ++控制台应用程序,在Windows上工作。

First I detached the console using FreeConsole , it works; 首先,我使用FreeConsole分离控制台,它工作; then, called AllocConsole as FreeConsole then AttachConsole not working suggested, returns true meaning success; 然后,将AllocConsole称为FreeConsole,然后AttachConsole不工作建议,返回true意思成功; last, I tried to attach it back using AttachConsole , but nothing happened -- 最后,我尝试使用AttachConsole将其附加回来,但没有任何反应 -

#include <psapi.h>

DWORD winpid = GetCurrentProcessId(); // get pid
std::cout << winpid; // it works    
FreeConsole(); // console lost
bool succeed = AllocConsole(); //succeeded.
succeed = AttachConsole(winpid); // return false: failed.
if (!succeed)
    LastError = GetLastError(); // Error Code 5

System Error Code 5 means : 系统错误代码5表示

ERROR_ACCESS_DENIED
5 (0x5)
Access is denied.

How shall I attach the console properly? 我该如何正确安装控制台?

Remove the AllocConsole call before AttachConsole. 在AttachConsole之前删除AllocConsole调用。

From the documentation : A process can be attached to at most one console. 文档 :一个进程最多可以附加到一个控制台。 If the calling process is already attached to a console, the error code returned is ERROR_ACCESS_DENIED (5). 如果调用进程已附加到控制台,则返回的错误代码为ERROR_ACCESS_DENIED(5)。

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

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