Unhook on terminate, to properly exit (Vista CMD.EXE and a MinGW-built
ansicon.exe would seem to exit okay, but the exit code was wrong).
Fixes #123.

Verify the DLL exists prior to injection.  With the DLL now being added
to the import table, failing to find it would prevent the process itself
from loading.
This commit is contained in:
Jason Hood 2018-11-04 11:17:23 +10:00
parent 0472db80b2
commit eccbbe7a54
5 changed files with 32 additions and 10 deletions

17
ANSI.c
View File

@ -215,6 +215,10 @@
always inject from ansicon.exe, even if it's GUI or excluded; always inject from ansicon.exe, even if it's GUI or excluded;
log CreateFile calls; log CreateFile calls;
preserve last error. preserve last error.
v1.86, 4 November, 2018:
always unhook, even on terminate;
check the DLL still exists before adding to imports.
*/ */
#include "ansicon.h" #include "ansicon.h"
@ -3026,6 +3030,17 @@ void Inject( DWORD dwCreationFlags, LPPROCESS_INFORMATION lpi,
} }
if (type > 0) if (type > 0)
{ {
#if defined(_WIN64) || defined(W32ON64)
if (type == 32)
*(PDWORD)DllNameType = 0x320033/*L'23'*/;
else
*(PDWORD)DllNameType = 0x340036/*L'46'*/;
#endif
if (GetFileAttributes( DllName ) == INVALID_FILE_ATTRIBUTES)
type = 0;
}
if (type > 0)
{
#ifdef _WIN64 #ifdef _WIN64
if (type == 64) if (type == 64)
{ {
@ -4122,7 +4137,6 @@ BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved )
if (lpReserved == NULL) if (lpReserved == NULL)
{ {
DEBUGSTR( 1, "Unloading" ); DEBUGSTR( 1, "Unloading" );
HookAPIAllMod( Hooks, TRUE, FALSE );
if (winmm != NULL) if (winmm != NULL)
FreeLibrary( winmm ); FreeLibrary( winmm );
} }
@ -4130,6 +4144,7 @@ BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved )
{ {
DEBUGSTR( 1, "Terminating" ); DEBUGSTR( 1, "Terminating" );
} }
HookAPIAllMod( Hooks, TRUE, FALSE );
if (orgattr != 0) if (orgattr != 0)
{ {
hConOut = CreateFile( L"CONOUT$", GENERIC_READ | GENERIC_WRITE, hConOut = CreateFile( L"CONOUT$", GENERIC_READ | GENERIC_WRITE,

View File

@ -99,7 +99,7 @@
add log level 32 to log CreateFile. add log level 32 to log CreateFile.
*/ */
#define PDATE L"23 August, 2018" #define PDATE L"4 November, 2018"
#include "ansicon.h" #include "ansicon.h"
#include "version.h" #include "version.h"

View File

@ -23,6 +23,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
#endif
#ifndef LOAD_LIBRARY_AS_IMAGE_RESOURCE #ifndef LOAD_LIBRARY_AS_IMAGE_RESOURCE
#define LOAD_LIBRARY_AS_IMAGE_RESOURCE 0x20 #define LOAD_LIBRARY_AS_IMAGE_RESOURCE 0x20
#endif #endif

View File

@ -3,7 +3,7 @@
Copyright 2005-2018 Jason Hood Copyright 2005-2018 Jason Hood
Version 1.85. Freeware Version 1.86. Freeware
Description Description
@ -340,6 +340,10 @@ Version History
Legend: + added, - bug-fixed, * changed. Legend: + added, - bug-fixed, * changed.
1.86 - 4 November, 2018:
- check the DLL exists before importing it (allows renaming to update);
- unhook on terminate, as well (fixes issues with Vista and MinGW).
1.85 - 23 August, 2018: 1.85 - 23 August, 2018:
- fix wrap issues with a buffer bigger than the window; - fix wrap issues with a buffer bigger than the window;
- fix -e et al when redirecting to NUL; - fix -e et al when redirecting to NUL;
@ -636,5 +640,5 @@ Distribution
in LICENSE.txt. in LICENSE.txt.
============================ =============================
Jason Hood, 23 August, 2018. Jason Hood, 4 November, 2018.

View File

@ -2,11 +2,11 @@
version.h - Version defines. version.h - Version defines.
*/ */
#define PVERS L"1.85" // wide string #define PVERS L"1.86" // wide string
#define PVERSA "1.85" // ANSI string (windres 2.16.91 didn't like L) #define PVERSA "1.86" // ANSI string (windres 2.16.91 didn't like L)
#define PVERE L"185" // wide environment string #define PVERE L"186" // wide environment string
#define PVEREA "185" // ANSI environment string #define PVEREA "186" // ANSI environment string
#define PVERB 1,8,5,0 // binary (resource) #define PVERB 1,8,6,0 // binary (resource)
#ifdef _WIN64 #ifdef _WIN64
# define BITS L"64" # define BITS L"64"