Utilisation de detours.h

Statut
N'est pas ouverte pour d'autres réponses.

neku

Codeur roumain
Dans ce post je vais donner un exemple sur l'utilisation de detours.h
Detours.h permet donc de faire un hook sur une API.

Dans cet exemple on va faire un hook sur la fonction printf(const char*, ...);
On aurais pu imaginer un hook sur glBegin dans une dll et l'injecter à n'importe quel moteur 3D utilisant OpenGL.

1) Creer une application console

Code:
#include <stdio.h>
#include <windows.h>
#include "detours.h"

DETOUR_TRAMPOLINE(int WINAPI Printf_Org(const char*, ...),
printf);

static int WINAPI Printf_New(const char* Format, ...)
{
	return (Printf_Org)("Printf function owned !\n");
}

int main(int argc, char* argv[])
{
	DetourFunctionWithTrampoline((PBYTE)Printf_Org, (PBYTE)Printf_New);

	printf("%s\n","test");

	system("pause");
	return 0;
}
Voila donc lorsque l'on appellera la fonction printf() elle ne retournera que "Printf function owned !\n"

Maintenant on peut refaire ce qu'elle fait habituellement.

Code:
static int WINAPI Printf_New(const char* Format, ...)
{
	va_list va_alist; 
	char Resultat[512];

    va_start (va_alist, Format); 
    _vsnprintf (Resultat, sizeof(Resultat), Format, va_alist); 
    va_end (va_alist);

	return (Printf_Org)(Resultat);
}
Pour ceux qui ne possedent pas detours.h et detours.lib
-> http://mvdesign.free.fr/detours.rar
 
Statut
N'est pas ouverte pour d'autres réponses.
Haut