Skip to content

Instantly share code, notes, and snippets.

@moccos
Last active December 22, 2015 12:39
Show Gist options
  • Save moccos/6473865 to your computer and use it in GitHub Desktop.
Save moccos/6473865 to your computer and use it in GitHub Desktop.
Windows console output test. Some functions fail when the stdout is redirected to a file.
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <wincon.h>
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR *lbuf = L"I love beef!\n";
CHAR *buf = "I love pork!\n";
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO scrInfo;
DWORD wroteSize;
GetConsoleScreenBufferInfo(hStdout, &scrInfo)
? printf("GetConsoleScreenBufferInfo: OK\n")
: printf("GetConsoleScreenBufferInfo: NG\n");
SetConsoleTextAttribute(hStdout, BACKGROUND_RED | FOREGROUND_BLUE)
? printf("SetConsoleTextAttribute: OK\n")
: printf("SetConsoleTextAttribute: NG\n");
WriteConsole(hStdout, lbuf, lstrlen(lbuf), &wroteSize, nullptr)
? printf("WriteConsole: OK\n")
: printf("WriteConsole: NG\n");
SetConsoleTextAttribute(hStdout, scrInfo.wAttributes);
WriteFile(hStdout, buf, strlen(buf), &wroteSize, nullptr)
? printf("WriteFile: OK\n")
: printf("WriteFile: NG\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment