在 C 中为 win32 和 win64 打开 Windows 控制台 stdin/stdout/stderr
Opening Windows console for stdin/stdout/stderr for both of win32 and win64 in C
首先,我不是 Windows 程序员(甚至不是 Windows 用户),我在 Linux 上使用交叉编译器来构建 Win32 和 Win64 .在搜索网络后(甚至在这里问了一个问题),我设法将一个代码片段放在一起,可以打开一个 windows 控制台,并将其用于 stdin/stdout/stderr。它在 Win32 上运行良好,但程序在 Win64 上崩溃。我猜问题是不同的长整数数据类型大小,gcc 甚至对此发出警告。但是,由于我不知道某些 windows API 类型的确切用途和大小,所以我不知道应该更改什么。当然,最好的办法是 win32/win64 独立的解决方案。我还尝试为 lStdHandle 使用 "HANDLE" 类型,但它甚至无法编译。有人可以帮忙吗?
int hConHandle;
long lStdHandle;
//HANDLE lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
FreeConsole(); // be sure to release possible already allocated console
if (!AllocConsole()) {
ERROR_WINDOW("Cannot allocate windows console!");
return;
}
SetConsoleTitle("My Nice Console");
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = 1024;
//coninfo.dwSize.X = 100;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
// redirect unbuffered STDOUT to the console
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
// redirect unbuffered STDIN to the console
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
// redirect unbuffered STDERR to the console
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
// Set Con Attributes
//SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
它是句柄,所以你应该使用HANDLE
类型。调用 _open_osfhandle
时转换为 INT_PTR
(如果您的 SDK 确实过时,则转换为 SIZE_T
),使用 long
会截断该值!
我不知道到底是什么问题。我没有设置在 Linux 上使用 MinGW 进行构建。如果您没有构建控制台应用程序,它可能与无效的文件描述符有关。在这种情况下,CRT 初始化文件描述符以将映射处理为无效句柄值,并且标准 FILE
流将被初始化为 -1 fileno
。但是我在你的代码中没有发现问题。
但是,您的 *stdin = *fp
hack 不是可移植的 C。它适用于旧版本的 MSVC,并且可能还适用于 msvcrt.dll(MinGW 由于缺乏更好的选择而有点可疑地使用)。但是,它不适用于新的通用 CRT。 AFILE
在新的CRT中定义如下:
typedef struct _iobuf
{
void* _Placeholder;
} FILE;
所以分配给 *stdin
只是覆盖这个 _Placeholder
指针。内部结构其实如下:
struct __crt_stdio_stream_data
{
union
{
FILE _public_file;
char* _ptr;
};
char* _base;
int _cnt;
long _flags;
long _file;
int _charbuf;
int _bufsiz;
char* _tmpfname;
CRITICAL_SECTION _lock;
};
所以你真正覆盖的只是它的缓冲区 _ptr
。
可移植地重新打开标准流的方法是通过 freopen
。所以我所做的是 freopen
NUL
设备,如果它是非控制台应用程序,它会将流重置为有效的文件描述符,这可行但也许其他人有更好的解决方案。然后使用 _dup2
重定向底层文件描述符。例如:
#include <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <Windows.h>
int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
//int wmain(int argc, wchar_t **argv)
{
int fdStd;
HANDLE hStd;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
printf("Goodbye, World!\n");
/* ensure references to current console are flushed and closed
* before freeing the console. To get things set up in case we're
* not a console application, first re-open the std streams to
* NUL with no buffering, and close invalid file descriptors
* 0, 1, and 2. The std streams will be redirected to the console
* once it's created. */
if (_get_osfhandle(0) < 0)
_close(0);
freopen("//./NUL", "r", stdin);
setvbuf(stdin, NULL, _IONBF, 0);
if (_get_osfhandle(1) < 0)
_close(1);
freopen("//./NUL", "w", stdout);
setvbuf(stdout, NULL, _IONBF, 0);
if (_get_osfhandle(2) < 0)
_close(2);
freopen("//./NUL", "w", stderr);
setvbuf(stderr, NULL, _IONBF, 0);
FreeConsole();
if (!AllocConsole()) {
//ERROR_WINDOW("Cannot allocate windows console!");
return 1;
}
SetConsoleTitle("My Nice Console");
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = 1024;
//coninfo.dwSize.X = 100;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
// redirect unbuffered STDIN to the console
hStd = GetStdHandle(STD_INPUT_HANDLE);
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT);
_dup2(fdStd, fileno(stdin));
SetStdHandle(STD_INPUT_HANDLE, (HANDLE)_get_osfhandle(fileno(stdin)));
_close(fdStd);
// redirect unbuffered STDOUT to the console
hStd = GetStdHandle(STD_OUTPUT_HANDLE);
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT);
_dup2(fdStd, fileno(stdout));
SetStdHandle(STD_OUTPUT_HANDLE, (HANDLE)_get_osfhandle(fileno(stdout)));
_close(fdStd);
// redirect unbuffered STDERR to the console
hStd = GetStdHandle(STD_ERROR_HANDLE);
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT);
_dup2(fdStd, fileno(stderr));
SetStdHandle(STD_ERROR_HANDLE, (HANDLE)_get_osfhandle(fileno(stderr)));
_close(fdStd);
// Set Con Attributes
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_GREEN | FOREGROUND_INTENSITY);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),
ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),
ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
printf("Hello, World!\n");
Sleep(10000);
return 0;
}
首先,我不是 Windows 程序员(甚至不是 Windows 用户),我在 Linux 上使用交叉编译器来构建 Win32 和 Win64 .在搜索网络后(甚至在这里问了一个问题),我设法将一个代码片段放在一起,可以打开一个 windows 控制台,并将其用于 stdin/stdout/stderr。它在 Win32 上运行良好,但程序在 Win64 上崩溃。我猜问题是不同的长整数数据类型大小,gcc 甚至对此发出警告。但是,由于我不知道某些 windows API 类型的确切用途和大小,所以我不知道应该更改什么。当然,最好的办法是 win32/win64 独立的解决方案。我还尝试为 lStdHandle 使用 "HANDLE" 类型,但它甚至无法编译。有人可以帮忙吗?
int hConHandle;
long lStdHandle;
//HANDLE lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
FreeConsole(); // be sure to release possible already allocated console
if (!AllocConsole()) {
ERROR_WINDOW("Cannot allocate windows console!");
return;
}
SetConsoleTitle("My Nice Console");
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = 1024;
//coninfo.dwSize.X = 100;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
// redirect unbuffered STDOUT to the console
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
// redirect unbuffered STDIN to the console
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
// redirect unbuffered STDERR to the console
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
// Set Con Attributes
//SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
它是句柄,所以你应该使用HANDLE
类型。调用 _open_osfhandle
时转换为 INT_PTR
(如果您的 SDK 确实过时,则转换为 SIZE_T
),使用 long
会截断该值!
我不知道到底是什么问题。我没有设置在 Linux 上使用 MinGW 进行构建。如果您没有构建控制台应用程序,它可能与无效的文件描述符有关。在这种情况下,CRT 初始化文件描述符以将映射处理为无效句柄值,并且标准 FILE
流将被初始化为 -1 fileno
。但是我在你的代码中没有发现问题。
但是,您的 *stdin = *fp
hack 不是可移植的 C。它适用于旧版本的 MSVC,并且可能还适用于 msvcrt.dll(MinGW 由于缺乏更好的选择而有点可疑地使用)。但是,它不适用于新的通用 CRT。 AFILE
在新的CRT中定义如下:
typedef struct _iobuf
{
void* _Placeholder;
} FILE;
所以分配给 *stdin
只是覆盖这个 _Placeholder
指针。内部结构其实如下:
struct __crt_stdio_stream_data
{
union
{
FILE _public_file;
char* _ptr;
};
char* _base;
int _cnt;
long _flags;
long _file;
int _charbuf;
int _bufsiz;
char* _tmpfname;
CRITICAL_SECTION _lock;
};
所以你真正覆盖的只是它的缓冲区 _ptr
。
可移植地重新打开标准流的方法是通过 freopen
。所以我所做的是 freopen
NUL
设备,如果它是非控制台应用程序,它会将流重置为有效的文件描述符,这可行但也许其他人有更好的解决方案。然后使用 _dup2
重定向底层文件描述符。例如:
#include <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <Windows.h>
int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
//int wmain(int argc, wchar_t **argv)
{
int fdStd;
HANDLE hStd;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
printf("Goodbye, World!\n");
/* ensure references to current console are flushed and closed
* before freeing the console. To get things set up in case we're
* not a console application, first re-open the std streams to
* NUL with no buffering, and close invalid file descriptors
* 0, 1, and 2. The std streams will be redirected to the console
* once it's created. */
if (_get_osfhandle(0) < 0)
_close(0);
freopen("//./NUL", "r", stdin);
setvbuf(stdin, NULL, _IONBF, 0);
if (_get_osfhandle(1) < 0)
_close(1);
freopen("//./NUL", "w", stdout);
setvbuf(stdout, NULL, _IONBF, 0);
if (_get_osfhandle(2) < 0)
_close(2);
freopen("//./NUL", "w", stderr);
setvbuf(stderr, NULL, _IONBF, 0);
FreeConsole();
if (!AllocConsole()) {
//ERROR_WINDOW("Cannot allocate windows console!");
return 1;
}
SetConsoleTitle("My Nice Console");
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = 1024;
//coninfo.dwSize.X = 100;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
// redirect unbuffered STDIN to the console
hStd = GetStdHandle(STD_INPUT_HANDLE);
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT);
_dup2(fdStd, fileno(stdin));
SetStdHandle(STD_INPUT_HANDLE, (HANDLE)_get_osfhandle(fileno(stdin)));
_close(fdStd);
// redirect unbuffered STDOUT to the console
hStd = GetStdHandle(STD_OUTPUT_HANDLE);
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT);
_dup2(fdStd, fileno(stdout));
SetStdHandle(STD_OUTPUT_HANDLE, (HANDLE)_get_osfhandle(fileno(stdout)));
_close(fdStd);
// redirect unbuffered STDERR to the console
hStd = GetStdHandle(STD_ERROR_HANDLE);
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT);
_dup2(fdStd, fileno(stderr));
SetStdHandle(STD_ERROR_HANDLE, (HANDLE)_get_osfhandle(fileno(stderr)));
_close(fdStd);
// Set Con Attributes
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_GREEN | FOREGROUND_INTENSITY);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),
ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),
ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
printf("Hello, World!\n");
Sleep(10000);
return 0;
}