So this is my first poor attempt at taking advantage of Windows Injection, I'm using the great example posted by PferdOne aka Flowerew in C++. I was hoping I could get some help on this, specifically in the Initialize function (to make sure I have the syntax correct) as well as helping me debug a compiler error. This is the DLL that will be injected, the injection code is working great :-)
Here is the source:
Code:
/*
Written by PferdOne aka Flowerew on 23rd Jan., 2010
Credits go to mmowned.com & gamedeception community for all the information.
Overview:
- Simple injector class to inject a dll into another process and load it
- Very simple EndScene hook method via VTable
- ClientConnection, CGCurMgr_C and CGObject_C included to show where it all begins
Define an environment variable with the name WOWDIR which points to your World of
Warcraft directory. That's where the dll will be put.
Note: Don't ask dumb questions. Your question has probably been answered a hundred
times. And by probably I mean most definitely. */
#include <stdint.h>
#include <windows.h>
HINSTANCE proc_addr = NULL;
//signed int __usercall sub_750230<eax>(int a1<ecx>, double a2<st0>, __int16 a3, int a4, unsigned int a5)
signed int *_real_receive_packet = NULL;
signed int _hook_receive_packet( int a1, double a2, int16_t a3, int a4, unsigned int a5);
#define RECEIVE_FUNC_ADDRESS 0x750230
void Initialize();
void Deinitialize();
//------------------------------------------------------------------------------
BOOL APIENTRY DllMain(HINSTANCE hInstDll, DWORD dwReason, LPVOID pReserved) {
proc_addr = hInstDll;
switch (dwReason) {
case DLL_PROCESS_ATTACH: {
OutputDebugString(TEXT("DLL_PROCESS_ATTACH\n"));
Initialize();
break; }
case DLL_THREAD_ATTACH: {
OutputDebugString(TEXT("DLL_THREAD_ATTACH\n"));
break; }
case DLL_PROCESS_DETACH: {
OutputDebugString(TEXT("DLL_PROCESS_DETACH\n"));
Deinitialize();
break; }
case DLL_THREAD_DETACH: {
OutputDebugString(TEXT("DLL_THREAD_DETACH\n"));
break; }
}
return TRUE;
}
signed int _hook_receive_packet( int a1, double a2, int16_t a3, int a4, unsigned int a5){
signed int res = (signed int)(*_real_receive_packet)( a1, a2, a3, a4, a5 );
// do something??
return res;
}
//------------------------------------------------------------------------------
void Initialize() {
unsigned int func_base = RECEIVE_FUNC_ADDRESS;
unsigned int *func_addr = reinterpret_cast<unsigned int*>(func_base);
_real_receive_packet = reinterpret_cast<int*>(*func_addr);
// simply overwrite the vtable function address with our hook func_addr
*func_addr = reinterpret_cast<unsigned int>(_hook_receive_packet);
}
//------------------------------------------------------------------------------
void Deinitialize() {
// unhook function
//unsigned int func_addr = *reinterpret_cast<unsigned int*>(RECEIVE_FUNC_ADDRESS);
//*func_addr = (unsigned int*)orig_receive_packet;
}
I'm getting an error when I actually try to call the _real_ function within the hooked function, the error is: "error C2064: term does not evaluate to a function taking 5 arguments". If I try to actually put the arguments near the declaration at the top of the code, it gives me a linking error. So I'm not entirely sure how to call a function just when you have the address.
Any help would be GREATLY appreciated! Thanks all!