Microsoft Detours-无法挂接__thiscall函数

Microsoft Detours - unable to hook __thiscall function

本文关键字:thiscall 函数 Detours- Microsoft      更新时间:2023-10-16

我正在尝试挂接一个未记录的函数,该函数具有以下签名:

(void(__thiscall*)(int arg1, int arg2))0x6142E0;

我看过弯路样本"成员",它解释道:

默认情况下,C++成员函数使用__thiscall调用习俗为了实现会员功能,蹦床并且迂回道必须与目标函数。遗憾的是,VC编译器不支持__因此,创建合法绕行和蹦床功能的唯一方法是让它们成为"绕行"类的类成员。

此外,C++不支持将指针转换为成员函数指向任意指针。要获取原始指针的必须移动到临时成员函数中指针,然后通过获取它的地址来传递,然后取消对它的引用。幸运的是,编译器会对代码进行优化,以删除额外的指针操作。

我已经从示例中复制了一些代码并对其进行了修改,但我似乎无法使其工作(此处为原始示例代码):

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)"函数,以便保留这里建议的"this pointer"。

尝试使用更强大的替代方案http://www.nektra.com/products/deviare-api-hook-windows/deviare-in-process/它是开源的。

Detours相当古老。显式编译器对__thiscall的支持是相当新的。Visual C++2005及更高版本似乎支持它。Detours的文件似乎从未更新过。