我正在做一些功能以方便制作钩子。我想将这些函数保留在一个文件中,并在需要时包含它。
我不知道它是否有效,但这不是问题。当我编译时,我收到此错误。我认为导入是错误的。
错误:
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);
}
答案 0 :(得分:2)
为了更好地理解:
您很可能会遇到多个定义错误,因为您同时#include
和链接同一个文件。
解决此问题的标准方法是拆分您的界面和实现,将声明放在一个受保护的头文件中,您将#include
和 definitions 放入您编译和链接的.cpp
文件。
对于您的示例,这可能如下所示:
#include "noxHook.hpp"
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;
}
#include <stdio.h>
#include "noxHook.hpp"
extern DWORD tpAddr = 0;
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);
}
#include <windows.h>
#define Naked __declspec( naked )
struct cHook
{
BYTE newBytes[6];;
BYTE oldBytes[6];
DWORD fromAddr;
LPVOID toAddr;
};
Naked void tpHook();
struct cHook SetupHook(DWORD fromAddr, LPVOID toAddr, LPVOID *oAddr);
void StartHook(struct cHook tmpHook);
void StopHook(struct cHook tmpHook);