I'm doing some functions to facilitate making hooks. I want to leave these functions in a file and include it when you need it.
I do not know if it's working yet but that's not the problem. When I compile, I get this error. I believe it is a mistake to import.
Error:
1>noxHook.obj : error LNK2005: "unsigned long tpAddr" (?tpAddr@@3KA) already defined in Hook.obj
1>noxHook.obj : error LNK2005: "void __cdecl StopHook(struct cHook)" (?StopHook@@YAXUcHook@@@Z) already defined in Hook.obj
1>noxHook.obj : error LNK2005: "void __cdecl StartHook(struct cHook)" (?StartHook@@YAXUcHook@@@Z) already defined in Hook.obj
1>noxHook.obj : error LNK2005: "struct cHook __cdecl SetupHook(unsigned long,void *,void * *)" (?SetupHook@@YA?AUcHook@@KPAXPAPAX@Z) already defined in Hook.obj
1>noxHook.obj : error LNK2005: "void __cdecl tpHook(void)" (?tpHook@@YAXXZ) already defined in Hook.obj
1>C:\Users\JorgeFranzon\Documents\Visual Studio 2008\Projects\Hook\Release\Hook.dll : fatal error LNK1169: one or more multiply defined symbols found
Hook.cpp:
#include "noxHook.cpp"
int(*tFunc)(int a, int b) = NULL;
int hkFunc(int a, int b)
{
printf("\n Hook: Original parameters: %d and %d\n", a, b);
return tFunc(98, 99);
}
void Main()
{
struct cHook hk1 = SetupHook(0x4016B0, hkFunc, &(PVOID&)tFunc);
StartHook(hk1);
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
Main();
break;
}
return TRUE;
}
noxHook.cpp:
#include <windows.h>
#include <stdio.h>
#define Naked __declspec( naked )
extern DWORD tpAddr = 0;
struct cHook
{
BYTE newBytes[6];;
BYTE oldBytes[6];
DWORD fromAddr;
LPVOID toAddr;
};
Naked void tpHook()
{
__asm NOP;
__asm NOP;
__asm NOP;
__asm NOP;
__asm NOP;
__asm NOP;
__asm PUSH tpAddr;
__asm RET;
}
struct cHook SetupHook(DWORD fromAddr, LPVOID toAddr, LPVOID *oAddr)
{
struct cHook tmpHook;
tmpHook.fromAddr = fromAddr;
tmpHook.toAddr = toAddr;
DWORD oldProt;
VirtualProtect((void*)fromAddr, 6, PAGE_EXECUTE_READWRITE, &oldProt);
tmpHook.newBytes[0] = 0x68;
memcpy(&tmpHook.newBytes[1], &toAddr, 4);
tmpHook.newBytes[5] = 0xC3;
memcpy(&tmpHook.oldBytes[0], &fromAddr, 6);
tpAddr = fromAddr + 6;
memcpy(&tpHook, &tmpHook.oldBytes[0], 6);
*oAddr = tpHook;
return tmpHook;
}
void StartHook(struct cHook tmpHook)
{
memcpy((void*)tmpHook.fromAddr, (void*)&tmpHook.newBytes, 6);
}
void StopHook(struct cHook tmpHook)
{
memcpy((void*)tmpHook.fromAddr, tmpHook.oldBytes, 6);
}