简体   繁体   English

DLL中的控制台输出窗口

[英]Console output window in DLL

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, ie 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, 我尝试了以下示例,这些示例是通过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! 提前致谢!

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

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 GetLastError return?) 话虽如此,一个进程一次只能与一个控制台关联,因此,如果该进程已经连接了一个控制台,那么分配新的控制台将无济于事(我假设您正在检查返回值)的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已加载到服务中,则该服务(可能)将在与当前登录用户不同的窗口站下运行,因此,如果您创建控制台窗口,则将无法看到它。

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

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