Skip to content

Commit d7a2d5e

Browse files
committed
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.
1 parent a1bf74d commit d7a2d5e

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

ANSI.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@
223223
v1.87, 3 February, 2019:
224224
some hooked functions are not imported, so myimport wasn't set;
225225
add missing SetCurrentConsoleFontEx to list of hooks.
226+
227+
v1.88, 1 March, 2019:
228+
a detached process has no console handle (fixes set_ansicon).
226229
*/
227230

228231
#include "ansicon.h"
@@ -3874,8 +3877,20 @@ void set_ansicon( PCONSOLE_SCREEN_BUFFER_INFO pcsbi )
38743877
hConOut = CreateFile( L"CONOUT$", GENERIC_READ | GENERIC_WRITE,
38753878
FILE_SHARE_READ | FILE_SHARE_WRITE,
38763879
NULL, OPEN_EXISTING, 0, NULL );
3877-
GetConsoleScreenBufferInfo( hConOut, &csbi );
3878-
CloseHandle( hConOut );
3880+
if (hConOut == INVALID_HANDLE_VALUE)
3881+
{
3882+
csbi.dwSize.X =
3883+
csbi.dwSize.Y =
3884+
csbi.srWindow.Left =
3885+
csbi.srWindow.Top =
3886+
csbi.srWindow.Right =
3887+
csbi.srWindow.Bottom = 0;
3888+
}
3889+
else
3890+
{
3891+
GetConsoleScreenBufferInfo( hConOut, &csbi );
3892+
CloseHandle( hConOut );
3893+
}
38793894
pcsbi = &csbi;
38803895
}
38813896

ansicon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
add log level 32 to log CreateFile.
100100
*/
101101

102-
#define PDATE L"3 February, 2019"
102+
#define PDATE L"1 March, 2019"
103103

104104
#include "ansicon.h"
105105
#include "version.h"

readme.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Copyright 2005-2019 Jason Hood
55

6-
Version 1.87. Freeware
6+
Version 1.88. Freeware
77

88

99
Description
@@ -340,6 +340,9 @@ Version History
340340

341341
Legend: + added, - bug-fixed, * changed.
342342

343+
1.88 - 1 March, 2019:
344+
- fix ANSICON environment variable when there is no console.
345+
343346
1.87 - 3 February, 2019:
344347
- fix crash when some programs start (bug during hooking);
345348
- properly hook SetCurrentConsoleFontEx.
@@ -644,5 +647,5 @@ Distribution
644647
in LICENSE.txt.
645648

646649

647-
=============================
648-
Jason Hood, 3 February, 2019.
650+
==========================
651+
Jason Hood, 1 March, 2019.

version.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
version.h - Version defines.
33
*/
44

5-
#define PVERS L"1.87" // wide string
6-
#define PVERSA "1.87" // ANSI string (windres 2.16.91 didn't like L)
7-
#define PVERE L"187" // wide environment string
8-
#define PVEREA "187" // ANSI environment string
9-
#define PVERB 1,8,7,0 // binary (resource)
5+
#define PVERS L"1.88" // wide string
6+
#define PVERSA "1.88" // ANSI string (windres 2.16.91 didn't like L)
7+
#define PVERE L"188" // wide environment string
8+
#define PVEREA "188" // ANSI environment string
9+
#define PVERB 1,8,8,0 // binary (resource)
1010

1111
#ifdef _WIN64
1212
# define BITS L"64"

0 commit comments

Comments
 (0)