[英]AllocConsole with Visual Studio 2013
I'm trying to have a console widow running alongside the main application window, apparently this should work and indeed the console window will show but the 3 lines of code starting with 'freopen' stop the compiling with errors such as "missing type specifier". 我正在尝试在主应用程序窗口旁边运行一个控制台寡妇,显然这应该可以工作,实际上控制台窗口会显示但是以'freopen'开头的3行代码会停止编译,例如“缺少类型说明符” 。
#include <stdio.h>
#include "Windows.h"
#include "Wincon.h"
BOOL f = AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
removing the 3 lines and replacing them with: 删除3行并将其替换为:
OutputDebugString(L"\n");
as I think should be used to set the text in the window gives the following errors: 我认为应该用于设置窗口中的文本给出以下错误:
1>main.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>main.cpp(13): error C2365: 'OutputDebugStringW' : redefinition; previous definition was 'function'
1> C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\winbase.h(7733) : see declaration of 'OutputDebugStringW'
1>main.cpp(13): error C2440: 'initializing' : cannot convert from 'const wchar_t [2]' to 'int'
1> There is no context in which this conversion is possible
searching for an answer gives plenty of results but they generally say: 寻找答案给出了很多结果,但他们通常说:
AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
and that just is not working for me so what am I missing? 那只是不适合我,所以我错过了什么?
I personally use this in my programs that need a console and a gui.. You can try it and see if it works for you.. It uses the iostream
's instead of freopen
. 我个人在需要控制台和gui的程序中使用它。你可以尝试一下,看它是否适合你..它使用的是
iostream
而不是freopen
。
It doesn't answer your question but it's an idea or another way of achieving the same goal. 它没有回答你的问题,但它是实现同一目标的想法或其他方式。
#include <iostream>
#include <windows.h>
#include <fstream>
#include <streambuf>
class Console
{
private:
std::wstreambuf *CinBuffer, *CoutBuffer, *CerrBuffer;
std::wfstream ConsoleInput, ConsoleOutput, ConsoleError;
public:
Console();
Console(const Console &console) = delete;
Console(Console&& console); = delete;
~Console();
Console& operator = (const Console& other) = delete;
Console& operator = (Console&& other) = delete;
template<typename T>
void operator << (const T &Data) {std::wcout<<Data<<std::flush;}
};
Console::Console()
{
if (AllocConsole())
{
CinBuffer = std::wcin.rdbuf();
CoutBuffer = std::wcout.rdbuf();
CerrBuffer = std::wcerr.rdbuf();
ConsoleInput.open("CONIN$", std::ios::in);
ConsoleOutput.open("CONOUT$", std::ios::out);
ConsoleError.open("CONOUT$", std::ios::out);
std::wcin.rdbuf(ConsoleInput.rdbuf());
std::wcout.rdbuf(ConsoleOutput.rdbuf());
std::wcerr.rdbuf(ConsoleError.rdbuf());
}
}
Console::~Console()
{
ConsoleInput.close();
ConsoleOutput.close();
ConsoleError.close();
std::wcin.rdbuf(CinBuffer);
std::wcout.rdbuf(CoutBuffer);
std::wcerr.rdbuf(CerrBuffer);
FreeConsole();
}
I will try to answer your question by posting code that is exactly what you've posted, but add the appropriate #include
files. 我将尝试通过发布您发布的代码来回答您的问题,但添加适当的
#include
文件。
#include <windows.h>
#include <cstdio>
int main()
{
BOOL f = AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
Using Visual Studio 2013, there are warnings concerning "dangerous function", but the code compiles successfully. 使用Visual Studio 2013,有关于“危险功能”的警告,但代码编译成功。 Therefore you should take the code above as-is and compile it.
因此,您应该按原样使用上面的代码并进行编译。 If there are errors, please update your question.
如果有错误,请更新您的问题。
Edit: Since you updated your question, the only reason I can see for those errors is because that code has to be within a function block. 编辑:由于您更新了问题,我可以看到这些错误的唯一原因是因为该代码必须位于功能块内。 You can't have executable lines of code just sitting by themselves like that.
你不能像这样只有自己的代码。
This is in essence what you posted: 这实际上就是你发布的内容:
#include <windows.h>
#include <cstdio>
BOOL f = AllocConsole();
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
This will not compile successfully due to the reasons above. 由于上述原因,这将无法成功编译。 The errors I get are similar to your errors.
我得到的错误与您的错误类似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.