This sounds like quite a good project I will have to check out this detouring library.
Edit:
A slight problem I am having.
I am trying to hook the API on windows CreateFileW.
Code:
HMODULE Kernel32 = GetModuleHandle("Kernel32.dll");
detour_CreateFileW = new MologieDetours::Detour<tCreateFileW>(Kernel32,"CreateFileW", hook_CreateFileW);
But my hook_CreateFileW is never accessed. The detour is put in place I can see it when I goto CreateFileW using OllyDBG however when I step into it, it appears to jump back to the original function straight away.
Here are my defines
Code:
typedef HANDLE ( WINAPI* tCreateFileW )(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile);
HANDLE WINAPI hook_CreateFileW(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile);
MologieDetours::Detour<tCreateFileW>* detour_CreateFileW = NULL;
I am using Visual Studio 2008 and compiling in Release Mode.
Is there anything I am doing wrong? Could you please give an example detouring a windows API.
I put a messagebox in the deconstructor of Detour and it showed. So it seems that the trampolean is having its JMP set to goto the backupOrigionalCode when using Detour(HMODULE module, const char* lpProcName, FunctionType pDetour).
Im going to try putting the exact address in instead of using this constructor to see if it makes a difference.
And it seems it does
Code:
detour_CreateFileW = new MologieDetours::Detour<tCreateFileW>((tCreateFileW)GetProcAddress(Kernel32,"CreateFileW"),hook_CreateFileW);
Works fine.
I think by using Detour() constructor inside the module one creates a tempoary Detour object which is destroyed when it is out of scope. Prehaps a fix for this would be to make a function such as SetDetour and just call that from each constructor passing propper paramaters.
Suggested fix
Code:
Copy current code from the first constructor into a private void CreateDetour( FunctionType pSource, FunctionType pDetour );
Make the constructors use this method rather than using another constructor.
I googled the problem I cannot post links so here is a search string
site:stackoverflow.com can-constructor-call-another-constructor-in-c
Other than that its a very nice clean library, its just waiting for more types of detours