windows DLL 中的控制台输出窗口

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

Console output window in DLL

c++windowsdebuggingconsole

提问by user303907

I am trying to redirect the output from my DLL to an external console window for easy debugging.

我正在尝试将 DLL 的输出重定向到外部控制台窗口以便于调试。

I have been told about AllocConsole but I am not able to reproduce it, i.e. the console window does not appear.

我听说过 AllocConsole,但我无法重现它,即控制台窗口没有出现。

My current environment is Visual Studio 2005.

我当前的环境是 Visual Studio 2005。

I tried the following example which is gotten off the Internet,

我尝试了以下从互联网上获取的示例,

AllocConsole();
HANDLE han = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsole(han,"hello",6,new DWORD,0);

yet nothing happens. Can someone point me in the right direction if creating a console window via DLL is possible in the first place.

然而什么也没有发生。如果首先可以通过 DLL 创建控制台窗口,有人可以指出我正确的方向吗?

Thanks in advance!

提前致谢!

回答by Ignacio Vazquez-Abrams

The proper way to output debug strings is via OutputDebugString(), with an appropriate debugging tool listening for output strings.

输出调试字符串的正确方法是 via OutputDebugString(),使用适当的调试工具监听输出字符串。

回答by Dean Harding

Once loaded, there is nothing special about DLLs, so there is no way that allocating consoles would be any different for a DLL than for the EXE that originally loaded it.

一旦加载,DLL 就没有什么特别之处了,因此对于 DLL 分配控制台与最初加载它的 EXE 没有任何不同。

Having said that, a process can be associated with only one console at a time, so if there is already a console attached to the process, then allocating a new one is not going to do anything (I assume you're checking the return value of AllocConsole? What does it return? What does GetLastErrorreturn?)

话虽如此,一个进程一次只能与一个控制台相关联,所以如果已经有一个控制台附加到该进程,那么分配一个新的控制台不会做任何事情(我假设您正在检查返回值的AllocConsole?它返回什么GetLastError?返回什么?)

There are some other possibilities. For example, if your DLL is loaded into a service, then the service will (likely) be running under a different window station to the currently logged-in user so if you create a console window, you won't be able to see it.

还有一些其他的可能性。例如,如果您的 DLL 被加载到服务中,那么该服务(可能)将在与当前登录用户不同的窗口站下运行,因此如果您创建一个控制台窗口,您将无法看到它.