From d7a2d5e962da3437d4b337bcfea0f8578257fdb2 Mon Sep 17 00:00:00 2001 From: Jason Hood Date: Fri, 1 Mar 2019 17:47:05 +1000 Subject: [PATCH] Fix ANSICON environment variable Creating a console handle in `set_ansicon` was assumed to succeed, but that is not the case when there is no console (if the process was started detached or freed its console). This left the console info uninitialised, causing my `printf` replacement to get stuck in a loop. Resolves #127. --- ANSI.c | 19 +++++++++++++++++-- ansicon.c | 2 +- readme.txt | 9 ++++++--- version.h | 10 +++++----- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/ANSI.c b/ANSI.c index d20ef47..8ef3d87 100644 --- a/ANSI.c +++ b/ANSI.c @@ -223,6 +223,9 @@ v1.87, 3 February, 2019: some hooked functions are not imported, so myimport wasn't set; add missing SetCurrentConsoleFontEx to list of hooks. + + v1.88, 1 March, 2019: + a detached process has no console handle (fixes set_ansicon). */ #include "ansicon.h" @@ -3874,8 +3877,20 @@ void set_ansicon( PCONSOLE_SCREEN_BUFFER_INFO pcsbi ) hConOut = CreateFile( L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); - GetConsoleScreenBufferInfo( hConOut, &csbi ); - CloseHandle( hConOut ); + if (hConOut == INVALID_HANDLE_VALUE) + { + csbi.dwSize.X = + csbi.dwSize.Y = + csbi.srWindow.Left = + csbi.srWindow.Top = + csbi.srWindow.Right = + csbi.srWindow.Bottom = 0; + } + else + { + GetConsoleScreenBufferInfo( hConOut, &csbi ); + CloseHandle( hConOut ); + } pcsbi = &csbi; } diff --git a/ansicon.c b/ansicon.c index 3becbf1..0fa742b 100644 --- a/ansicon.c +++ b/ansicon.c @@ -99,7 +99,7 @@ add log level 32 to log CreateFile. */ -#define PDATE L"3 February, 2019" +#define PDATE L"1 March, 2019" #include "ansicon.h" #include "version.h" diff --git a/readme.txt b/readme.txt index 9f90b10..b3fdc2b 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Copyright 2005-2019 Jason Hood - Version 1.87. Freeware + Version 1.88. Freeware Description @@ -340,6 +340,9 @@ Version History Legend: + added, - bug-fixed, * changed. + 1.88 - 1 March, 2019: + - fix ANSICON environment variable when there is no console. + 1.87 - 3 February, 2019: - fix crash when some programs start (bug during hooking); - properly hook SetCurrentConsoleFontEx. @@ -644,5 +647,5 @@ Distribution in LICENSE.txt. -============================= -Jason Hood, 3 February, 2019. +========================== +Jason Hood, 1 March, 2019. diff --git a/version.h b/version.h index b89bdb7..1aca643 100644 --- a/version.h +++ b/version.h @@ -2,11 +2,11 @@ version.h - Version defines. */ -#define PVERS L"1.87" // wide string -#define PVERSA "1.87" // ANSI string (windres 2.16.91 didn't like L) -#define PVERE L"187" // wide environment string -#define PVEREA "187" // ANSI environment string -#define PVERB 1,8,7,0 // binary (resource) +#define PVERS L"1.88" // wide string +#define PVERSA "1.88" // ANSI string (windres 2.16.91 didn't like L) +#define PVERE L"188" // wide environment string +#define PVEREA "188" // ANSI environment string +#define PVERB 1,8,8,0 // binary (resource) #ifdef _WIN64 # define BITS L"64"