AllocConsole()可以创建控制台程序,但是每个进程只能拥有一个控制台窗口。
MSDN原文如下:
Remarks
A process can be associated with only one console, so the
AllocConsole function fails if the calling process already has
a console. A process can use the FreeConsole function to detach itself from its
current console, then it can call AllocConsole to create a new
console or AttachConsole to attach to another console.
如果只是想在线程中使用控制台,可以使用如下代码:
如果已经在其他线程中创建了控制台窗口且该控制台窗口没有被销毁的情况下,使用AllocConsole函数再次创建,则该函数会返回0。