Skip to content

Commit

Permalink
Preserve last error
Browse files Browse the repository at this point in the history
Hooked functions that failed and logged debug messages were causing an
incorrect last error to be set.
  • Loading branch information
adoxa committed Aug 23, 2018
1 parent 06459ed commit 0472db8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
27 changes: 20 additions & 7 deletions ANSI.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@
v1.85, 22 & 23 August, 2018:
fix creating the wrap buffer;
always inject from ansicon.exe, even if it's GUI or excluded;
log CreateFile calls.
log CreateFile calls;
preserve last error.
*/

#include "ansicon.h"
Expand Down Expand Up @@ -3113,7 +3114,9 @@ BOOL WINAPI MyCreateProcessA( LPCSTR lpApplicationName,
lpStartupInfo,
&child_pi ))
{
DEBUGSTR( 1, " Failed (%u)", GetLastError() );
DWORD err = GetLastError();
DEBUGSTR( 1, " Failed (%u)", err );
SetLastError( err );
return FALSE;
}

Expand Down Expand Up @@ -3151,7 +3154,9 @@ BOOL WINAPI MyCreateProcessW( LPCWSTR lpApplicationName,
lpStartupInfo,
&child_pi ))
{
DEBUGSTR( 1, " Failed (%u)", GetLastError() );
DWORD err = GetLastError();
DEBUGSTR( 1, " Failed (%u)", err );
SetLastError( err );
return FALSE;
}

Expand Down Expand Up @@ -3222,17 +3227,21 @@ FARPROC WINAPI MyGetProcAddress( HMODULE hModule, LPCSTR lpProcName )
HMODULE WINAPI MyLoadLibraryA( LPCSTR lpFileName )
{
HMODULE hMod = LoadLibraryA( lpFileName );
DEBUGSTR( 2, "LoadLibraryA %s", lpFileName );
DWORD err = GetLastError();
DEBUGSTR( 2, "LoadLibraryA %\"s", lpFileName );
HookAPIAllMod( Hooks, FALSE, TRUE );
SetLastError( err );
return hMod;
}


HMODULE WINAPI MyLoadLibraryW( LPCWSTR lpFileName )
{
HMODULE hMod = LoadLibraryW( lpFileName );
DEBUGSTR( 2, "LoadLibraryW %S", lpFileName );
DWORD err = GetLastError();
DEBUGSTR( 2, "LoadLibraryW %\"S", lpFileName );
HookAPIAllMod( Hooks, FALSE, TRUE );
SetLastError( err );
return hMod;
}

Expand All @@ -3245,8 +3254,10 @@ HMODULE WINAPI MyLoadLibraryExA( LPCSTR lpFileName, HANDLE hFile,
LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE |
LOAD_LIBRARY_AS_IMAGE_RESOURCE)))
{
DEBUGSTR( 2, "LoadLibraryExA %s", lpFileName );
DWORD err = GetLastError();
DEBUGSTR( 2, "LoadLibraryExA %\"s", lpFileName );
HookAPIAllMod( Hooks, FALSE, TRUE );
SetLastError( err );
}
return hMod;
}
Expand All @@ -3260,8 +3271,10 @@ HMODULE WINAPI MyLoadLibraryExW( LPCWSTR lpFileName, HANDLE hFile,
LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE |
LOAD_LIBRARY_AS_IMAGE_RESOURCE)))
{
DEBUGSTR( 2, "LoadLibraryExW %S", lpFileName );
DWORD err = GetLastError();
DEBUGSTR( 2, "LoadLibraryExW %\"S", lpFileName );
HookAPIAllMod( Hooks, FALSE, TRUE );
SetLastError( err );
}
return hMod;
}
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ Version History
- fix -e et al when redirecting to NUL;
- prevent -p from injecting when already injected;
- fix running directly via ansicon (hook even if it's GUI or excluded);
- preserve last error;
+ add log level 32 to monitor CreateFile.

1.84 - 11 May, 2018:
Expand Down

0 comments on commit 0472db8

Please sign in to comment.