问题描述
我在Visual Studio 2013中使用C ++控制台应用程序,在Windows上工作。
I'm working with a C++ Console Application in Visual Studio 2013, working on Windows.
首先我使用 FreeConsole卸载控制台
,它工作;然后,将 AllocConsole
命名为建议,返回 true
意味着成功;最后,我尝试使用 AttachConsole
附加它,但没有发生任何事 -
First I detached the console using FreeConsole
, it works; then, called AllocConsole
as FreeConsole then AttachConsole not working suggested, returns true
meaning success; last, I tried to attach it back using AttachConsole
, but nothing happened --
#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
:
ERROR_ACCESS_DENIED
5 (0x5)
Access is denied.
如何正确连接控制台?
推荐答案
在AttachConsole之前删除AllocConsole调用。
Remove the AllocConsole call before AttachConsole.
从:
一个进程最多可以附加到一个控制台。如果调用进程已附加到控制台,则返回的错误代码为ERROR_ACCESS_DENIED(5)。
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).
这篇关于AttachConsole错误5:访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!