Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Created November 24, 2013 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoehrmann/7629616 to your computer and use it in GitHub Desktop.
Save hoehrmann/7629616 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
int
main(int argc, char *argv[]) {
char* buffer;
BOOL bStatus;
COORD coords;
HANDLE hConsole;
DWORD dwRead, dwLength;
DWORD dwCursorOffset, dwStartOffset;
CONSOLE_SCREEN_BUFFER_INFOEX info;
FILE* outputFile;
if (argc == 1 || argc > 2) {
fprintf(stderr, "Usage: %s /path/to/file\n", argv[0]);
exit(0);
}
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!hConsole || hConsole == INVALID_HANDLE_VALUE) {
fprintf(stderr, "ERORR: failed to obtain standard handle\n");
}
info.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
bStatus = GetConsoleScreenBufferInfoEx(hConsole, &info);
if (bStatus) {
}
dwCursorOffset = info.dwMaximumWindowSize.X
* info.dwCursorPosition.Y + info.dwCursorPosition.X;
dwStartOffset = max(0,
dwCursorOffset - min(dwCursorOffset, 65535));
dwLength = dwCursorOffset - dwStartOffset;
// TODO: overflow? align to start of line?
coords.X = (dwStartOffset % info.dwMaximumWindowSize.X);
coords.Y = (dwStartOffset / info.dwMaximumWindowSize.X);
buffer = (char*)malloc(dwLength);
if (!buffer) {
}
bStatus = ReadConsoleOutputCharacterA(hConsole, (LPSTR)buffer,
dwLength, coords, &dwRead);
if (bStatus) {
}
outputFile = fopen(argv[1], "wb");
if (outputFile) {
size_t written = fwrite(buffer, sizeof(*buffer), dwLength, outputFile);
if (written != dwLength) {
}
fclose(outputFile);
} else {
fprintf(stderr, "ERROR: failed to open output file\n");
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment