I created a very simple dll that would intercept fprintf calls and print them again using vfprintf (in the end I will add functionality to that code, but for now this is outside of the scope of the problem).
See the code below.
If I remove the line #define WORKS and recompile the executable that got this dll injected into it will simply crash !
The difference between the working version and the crashing version is:
- when I use vfprintf directly it crashes
- after I Hooked vfprintf too (still don't know why I decided to try that) and then call the "next" function in the hook, it works perfectly
Altough the workaround works, I would like to understand why I need to hook vfprintf too.
Thanks,
-- Geert
Code: Select all
// ***************************************************************
// InterceptFprintf.dll version: 1.0 · date: 2004-08-12
// -------------------------------------------------------------
// this dll intercepts fprintf
// ***************************************************************
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
#include "madCHook.h"
#define WORKS
// ***************************************************************
int ( *ori_fprintf ) (FILE *stream, const char *format, ...);
#ifdef WORKS
int ( *ori_vfprintf) (FILE *stream, const char *format, va_list argptr);
#endif
// ***************************************************************
#ifdef WORKS
static int catch_vfprintf(FILE *stream, const char *format, va_list argptr)
{
return(ori_vfprintf(stream, format, argptr));
}
#endif
static int catch_fprintf(FILE *stream, const char *format, ...)
{
va_list args;
va_start(args, format);
#ifdef WORKS
return(ori_vfprintf (stream, format, args));
#else
return(vfprintf (stream, format, args));
#endif
}
// ***************************************************************
BOOL WINAPI DllMain(HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH) {
// InitializeMadCHook is needed only if you're using the static madCHook.lib
InitializeMadCHook();
#ifdef WORKS
HookAPI("msvcrt.dll", "vfprintf", catch_vfprintf, (PVOID*) &ori_vfprintf);
#else
HookAPI("msvcrt.dll", "fprintf", catch_fprintf, (PVOID*) &ori_fprintf);
#endif
} else if (fdwReason == DLL_PROCESS_DETACH)
// FinalizeMadCHook is needed only if you're using the static madCHook.lib
FinalizeMadCHook();
return true;
}