1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| /*CaptureConsoleCode.c*/
#define _WIN32_WINNT 0x501
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <tchar.h>
#include "CaptureConsoleCode.h"
static const wchar_t g_htmlCGAColors[16][4] = {
L"000", L"00A", L"0A0", L"0AA", L"A00", L"A0A", L"A50", L"AAA",
L"555", L"55F", L"5F5", L"5FF", L"F55", L"F5F", L"FF5", L"FFF"
};
static const wchar_t g_htmlConsoleColors[16][7] = {
L"000000", L"000080", L"008000", L"008080", L"800000", L"800080", L"808000", L"C0C0C0",
L"808080", L"0000FF", L"00FF00", L"00FFFF", L"FF0000", L"FF00FF", L"FFFF00", L"FFFFFF"
};
/*
===============================================================================
Util functions used in saving */
static LPCTSTR FindLastOfAnyC(LPCTSTR str, LPCTSTR chars)
{
LPCTSTR ret = NULL;
for( ; (*str) != _T('\0') ; str++)
{
if(_tcschr(chars, *str) != NULL)
ret = str;
}
return ret;
}
/*
-----------------------------------------------------------------------------*/
static LPTSTR GetFileNameWithoutExtension(LPCTSTR filePath)
{
LPTSTR ret = NULL;
if(filePath!=NULL)
{
LPCTSTR lastSep = FindLastOfAnyC(filePath, _T("\\/"));
if(lastSep!=NULL)
{
LPCTSTR fileName = lastSep+1;
LPCTSTR ext = _tcsrchr(filePath, _T('.'));
size_t cchNameLength = (ext==NULL ? _tcslen(fileName) : ext-fileName);
size_t cchNameSize = cchNameLength+1;
ret = calloc(sizeof *ret, cchNameSize);
if(ret != NULL)
_tcsncpy_s(ret, cchNameSize, fileName, _TRUNCATE); /*This will truncate where the extension starts.*/
}
}
return ret;
}
void FreeFileName(LPTSTR buf) { free(buf); }
/*
=============================================================================
Saving functions */
static int WriteColorSpanTag(FILE *pfOut, BYTE attr)
{
BYTE fg = attr & 0x0F;
BYTE bg = (attr >> 4) & 0x0F;
if(bg==0)
return fwprintf(pfOut, L"<span style=\"color:#%s\">", g_htmlConsoleColors[fg]);
else
return fwprintf(pfOut, L"<span style=\"background-color:#%s; color:#%s\">", g_htmlConsoleColors[bg], g_htmlConsoleColors[fg]);
}
/*Write a character while stripping the bare minimum of HTML entities.*/
static int WriteHtmlChar(FILE *pfOut, wchar_t c)
{
switch(c)
{
case L'<': return fputws(L"<", pfOut);
case L'>': return fputws(L">", pfOut);
case L'&': return fputws(L"&", pfOut);
default: return fputwc(c, pfOut);
}
}
/*
count all lines that have at least one non-space character.*/
static int CountLines(CHAR_INFO const *outputChars, short cx, short cy)
{
int ymax = 0;
int x;
int y;
for(y=0 ; y<cy ; y++)
{
for(x=0 ; x<cx ; x++)
{
wchar_t c = outputChars[y*cx + x].Char.UnicodeChar;
if(c != L' ')
{
ymax = y;
break;
}
}
}
return ymax;
}
/*
-----------------------------------------------------------------------------*/
BOOL SaveConsoleCaptureToFile(LPCTSTR filePath, CHAR_INFO const *outputChars, short cx, short cy)
{
/*Open the output file*/
FILE *pfOut = NULL;
errno_t err = (filePath==NULL || outputChars==NULL)
? EINVAL
: _tfopen_s(&pfOut, filePath, _T("w, ccs=UTF-8"));
if(err != 0)
{
switch(err)
{
case EPERM:
case EACCES: SetLastError(ERROR_ACCESS_DENIED); break;
case ENOENT: SetLastError(ERROR_FILE_NOT_FOUND); break;
case EIO: SetLastError(ERROR_IO_DEVICE); break;
case EISDIR: SetLastError(ERROR_DIRECTORY); break;
case EINVAL: SetLastError(ERROR_INVALID_PARAMETER); break;
default: SetLastError(ERROR_FUNCTION_FAILED); break;
}
return FALSE;
}
fputws(L"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n", pfOut);
fputws(L"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n", pfOut);
/*Write the head, with the file name in the title.*/
{
LPTSTR fileNameBuf = GetFileNameWithoutExtension(filePath);
fputws(L"<head>\n", pfOut);
fputws(L"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n", pfOut);
if(fileNameBuf != NULL)
{
size_t iTitleChar;
fputws(L"<title>", pfOut);
for(iTitleChar=0 ; fileNameBuf[iTitleChar] != L'\0' ; iTitleChar++)
WriteHtmlChar(pfOut, fileNameBuf[iTitleChar]);
fputws(L" - Console capture</title>\n", pfOut);
FreeFileName(fileNameBuf);
}
fputws(L"</head>\n", pfOut);
}
/*Write the body, with the output characters.*/
{
/*First, count all lines that have at least one non-space character.*/
int ymax = CountLines(outputChars, cx, cy);
int y;
fputws(L"<body style=\"background-color: #000; color: #AAA;\">\n", pfOut);
fputws(L"<pre style=\"font-family: 'Lucida Console',monospace; font-size: 10pt\">\n", pfOut);
for(y=0 ; y<ymax+1 ; y++)
{
/*Color is set with spans for each line.*/
int x;
BYTE lastAttr = (BYTE)(outputChars[y*cx + 0].Attributes & 0x00FF);
WriteColorSpanTag(pfOut, lastAttr);
for(x=0 ; x<cx ; x++)
{
wchar_t c = outputChars[y*cx + x].Char.UnicodeChar;
/*Don't write trailing spaces: For each space, check if it's trailing.*/
if(c == L' ')
{
/*Try to find any non-space character on this line*/
int x2;
for(x2=x ; x2<cx ; x2++)
{
if(outputChars[y*cx + x2].Char.UnicodeChar != L' ')
break;
}
/*If found one, proceed normally. If not, break from x loop.*/
if(x2==cx)
break;
}
/*Handle color change*/
{
BYTE newAttr = (BYTE)(outputChars[y*cx + x].Attributes & 0x00FF);
if(newAttr != lastAttr)
{
fputws(L"</span>", pfOut);
lastAttr = newAttr;
WriteColorSpanTag(pfOut, lastAttr);
}
}
WriteHtmlChar(pfOut, c);
}
/*End of line.*/
fputws(L"</span>\n", pfOut);
}
fputws(L"</pre>\n", pfOut);
fputws(L"</body>\n", pfOut);
}
fputws(L"</html>\n", pfOut);
fclose(pfOut);
return TRUE;
}
/*
===============================================================================
Capture functions */
BOOL CaptureConsole(DWORD targetProcessId, CHAR_INFO **ppOutputChars, COORD *pSize)
{
BOOL ret = FALSE;
if(ppOutputChars == NULL || pSize == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
*ppOutputChars = NULL;
if(!AttachConsole(targetProcessId))
{
DWORD err = GetLastError();
(void)err;
}
else
{
HANDLE hOutput = CreateFile(_T("CONOUT$"), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hOutput != INVALID_HANDLE_VALUE)
{
CONSOLE_SCREEN_BUFFER_INFO info = {0};
BOOL bGotInfo = GetConsoleScreenBufferInfo(hOutput, &info);
*pSize = info.dwSize;
if(bGotInfo)
{
int outputSize = info.dwSize.X * info.dwSize.Y;
CHAR_INFO* pOutputChars = calloc(outputSize, sizeof *pOutputChars);
if(pOutputChars != NULL)
{
/*Read by blocks of incY lines, because there was something that made reading as a whole block fail.*/
BOOL bReadChars = TRUE;
const short incY = 50;
short blockY;
for(blockY=0 ; blockY<info.dwSize.Y ; blockY+=incY)
{
SMALL_RECT rgn = {0, blockY, info.dwSize.X-1, min(blockY+incY, info.dwSize.Y)-1};
COORD origin= {0, blockY};
bReadChars = bReadChars && ReadConsoleOutputW(hOutput, pOutputChars, info.dwSize, origin, &rgn);
}
if(bReadChars) {
ret = TRUE; /*Success!*/
} else {
DWORD err = GetLastError();
(void)err;
}
/*Clean up.*/
if(!ret)
free(pOutputChars), pOutputChars=NULL;
*ppOutputChars = pOutputChars;
}
else
SetLastError(ERROR_OUTOFMEMORY);
}
CloseHandle(hOutput), hOutput=INVALID_HANDLE_VALUE;
}
FreeConsole();
}
return ret;
}
void FreeOutputChars(CHAR_INFO *pOutputChars) { free(pOutputChars); }
/*
===============================================================================
Main functions */
/* Capture console from target process and save it as HTML.
hWndOwner : HWND of owner window for dialog boxes.
targetProcessId : ID of the process whose console to capture.
*/
BOOL CaptureAndSaveConsole(DWORD targetProcessId, LPCTSTR filePath)
{
BOOL ret = FALSE;
CHAR_INFO *pOutputChars = NULL;
COORD size = {0};
if(CaptureConsole(targetProcessId, &pOutputChars, &size))
{
/*Save this to a HTML file.*/
ret = SaveConsoleCaptureToFile(filePath, pOutputChars, size.X, size.Y);
FreeOutputChars(pOutputChars);
}
return ret;
} |
Partager