【发布时间】:2015-12-15 23:19:14
【问题描述】:
我正在尝试挂钩具有签名的未记录函数:
(void(__thiscall*)(int arg1, int arg2))0x6142E0;
我查看了 detours 示例“成员”,其中解释了:
默认情况下,C++ 成员函数使用 __thiscall 调用 惯例。为了绕开一个成员函数,蹦床 并且 detour 必须具有与 目标函数。不幸的是,VC 编译器不支持 __thiscall,因此创建合法的 detour 和 trampoline 函数的唯一方法是让它们成为“detour”类的类成员。
另外,C++ 不支持将指针转换为成员 函数指向任意指针。要获取原始指针,地址 成员函数的一部分必须移动到临时成员函数中 指针,然后通过获取它的地址传递,然后取消引用它。 幸运的是,编译器会优化代码以去除多余的 指针操作。
我已经从示例中复制了一些代码并对其进行了修改,但我似乎无法让它工作(original example code here):
class CDetour {
public:
void Mine_Target(int arg1, int arg2);
static void (CDetour::* Real_Target)(int arg1, int arg2);
};
void CDetour::Mine_Target(int arg1, int arg2) {
printf(" CDetour::Mine_Target! (this:%p)\n", this);
(this->*Real_Target)(arg1, arg2);
}
void (CDetour::* CDetour::Real_Target)(int arg1, int arg2) = (void(CDetour::*)(int arg1, int arg2)) (0x6142E0);
void hoo()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)CDetour::Real_Target, (PVOID)(&(PVOID&)CDetour::Mine_Target));
DetourTransactionCommit();
}
我不知道如何让它工作。 a bow 代码有两个编译器错误:
void (CDetour::* CDetour::Real_Target)(int arg1, int arg2) = (void(CDetour::*)(int arg1, int arg2)) (0x6142E0);
//Error C2440 'type cast': cannot convert from 'int' to 'void (__thiscall CDetour::* )(int,int)'
和:
DetourAttach(&(PVOID&)CDetour::Real_Target, (PVOID)(&(PVOID&)CDetour::Mine_Target));
//Error C2440 'type cast': cannot convert from 'void (__thiscall CDetour::* )(int,int)' to 'PVOID &'
我希望有人能在正确的方向上帮助我,因为我即将放弃挂钩 __thiscall 函数...
我正在考虑使用内联汇编编写一个全局“__declspec(naken) void MyFunc(int, int)”函数,以便按照here 的建议保留“this 指针”。
【问题讨论】: