C# API 函数 AllocConsole 和 AttachConsole(-1) 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/432832/
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
What is the different between API functions AllocConsole and AttachConsole(-1)?
提问by abatishchev
Could you please explain me, what is the different between API functions AllocConsole
and AttachConsole(-1)
? I mean if AttachConsole
gets ATTACH_PARENT_PROCESS(DWORD)-1
.
你能解释一下,API 函数AllocConsole
和AttachConsole(-1)
? 我的意思是如果AttachConsole
得到ATTACH_PARENT_PROCESS(DWORD)-1
.
采纳答案by Michael Burr
Well, the fundamental difference is:
好吧,根本区别在于:
AllocConsole()
will create a new console (and attach to it)AttachConsole( ATTACH_PARENT_PROCESS /* -1 */)
will not create a new console, it will attach to the existing console of the parent process.
AllocConsole()
将创建一个新的控制台(并附加到它)AttachConsole( ATTACH_PARENT_PROCESS /* -1 */)
不会创建新的控制台,它将附加到父进程的现有控制台。
In the first case you get a whole new console window, in the second case, you use an existing console window.
在第一种情况下,您将获得一个全新的控制台窗口,在第二种情况下,您将使用现有的控制台窗口。
Of course, if you're already attached to a console (ie., you're a console mode program launched from cmd.exe) there's not much difference - you'll get an error with either API.
当然,如果您已经连接到控制台(即,您是从 cmd.exe 启动的控制台模式程序),则没有太大区别 - 使用任一 API 都会出错。
Also note that just because you detach from a console doesn't mean the detached console will be useful - for example, if you're a console process launched from a cmd window, that window essentially blocks until your process ends.
另请注意,仅仅因为您从控制台分离并不意味着分离的控制台会有用 - 例如,如果您是从 cmd 窗口启动的控制台进程,则该窗口基本上会阻塞,直到您的进程结束。
Some code to play with:
一些代码可以玩:
int main( int argc, char* argv[])
{
int ch;
BOOL bResult;
printf( "default console\n");
ch = getchar();
bResult = FreeConsole();
bResult = AllocConsole();
printf( "AllocConsole()\n");
ch = getchar();
bResult = FreeConsole();
bResult = AttachConsole( ATTACH_PARENT_PROCESS);
printf( "AttachConsole( ATTACH_PARENT_PROCESS)\n");
ch = getchar();
return 0;
}
回答by Matthias van der Vlies
It has been a while since I used the winapi, but I looked up the MSDN documentationand I was not able to find the CreateConsole API function. So my guess is that CreateConsole is legacy stuff and has been replaced by AttachConsole. So there is probably no difference, but CreateConsole has probably been deprecated.
好久没用winapi了,但是查了MSDN文档,没找到CreateConsole API函数。所以我的猜测是 CreateConsole 是遗留的东西,已经被 AttachConsole 取代。所以可能没有区别,但 CreateConsole 可能已被弃用。
回答by Zach Hirsch
I don't think there's a function called CreateConsole
, but there's AllocConsole
.
我认为没有名为 的函数CreateConsole
,但有AllocConsole
.
Assuming that's what you meant, I think the difference is that AttachConsole(ATTACH_PARENT_PROCESS)
can return ERROR_INVALID_HANDLE
if the parent process doesn't have a console.
假设这就是您的意思,我认为不同之处在于,如果父进程没有控制台,则AttachConsole(ATTACH_PARENT_PROCESS)
可以返回ERROR_INVALID_HANDLE
。
Try running this code from both a command prompt and Start -> Run:
尝试从命令提示符和开始 -> 运行运行此代码:
#include <windows.h>
#pragma comment ( lib, "user32.lib" )
int main()
{
BOOL b;
char msg[1024];
b = FreeConsole();
sprintf(msg, "%d", b);
MessageBox(NULL, msg, "FreeConsole", 0);
b = AttachConsole(ATTACH_PARENT_PROCESS);
sprintf(msg, "%d", b);
MessageBox(NULL, msg, "AttachConsole", 0);
return 0;
}
When run from a command prompt, two message boxes containing a 1
are displayed, meaning both calls succeeded. When run from Start -> Run, the first box contains 1
and the second contains 0
, meaning that only the first call succeeded. The second one fails because explorer.exe (which is the parent of a process launched from Start -> Run) doesn't have a console.
从命令提示符运行时,1
会显示两个包含 的消息框,这意味着两个调用都成功了。当从 Start -> Run 运行时,第一个框包含1
,第二个包含0
,这意味着只有第一次调用成功。第二个失败,因为 explorer.exe(它是从开始 -> 运行启动的进程的父进程)没有控制台。