C++挂钩成员函数和原始函数返回垃圾值。

C++挂钩成员函数和原始函数返回垃圾值。,c++,winapi,dll,hook,detours,C++,Winapi,Dll,Hook,Detours,我想要使用dll注入的钩子类成员函数。现在,挂钩就是成功。 但在挂钩函数中,我用return编写了调用原始函数的代码。 结果,原始函数返回垃圾值。我的工作怎么了 target.cpp #include <iostream> #include <windows.h> class Target { private: int _member; public: Target() : _member(0)

我想要使用dll注入的钩子类成员函数。现在,挂钩就是成功。 但在挂钩函数中,我用return编写了调用原始函数的代码。 结果,原始函数返回垃圾值。我的工作怎么了

target.cpp

#include <iostream>
#include <windows.h>

class Target
{
    private:
        int _member;

    public:
        Target()
        : _member(0)
        {
        }

        Target(int i)
        : _member(i)
        {
        }

        virtual ~Target()
        {
        }

        int getValue() // I want to hooking this function.
        {
            return _member;
        }
};

int main(int argc, char **argv)
{
    while(1){
        Sleep(10000);        
        Target objA, objB(7);
        std::cout << objA.getValue() << " " << objB.getValue() << std::endl;
    }
    return 0; 
}
injection.dll

#include <windows.h>
#include <detours.h>
#include <iostream>
#include <sstream>

#pragma comment(lib, "detours.lib")

static const int ADDRESS = 0x2180;
int (__thiscall * original_func)(void *);

int hookedFunction(void *obj)
{
    uintptr_t base = (uintptr_t)GetModuleHandle(0);
    std::cout << obj << " Hooked Obj Address.\n";
    int result = original_func(obj);
    std::cout << "original function returns " << result << '\n';
    return result; 
}

DWORD WINAPI Attach(LPVOID param)
{
    uintptr_t base = (uintptr_t)GetModuleHandle(0);

    original_func = (decltype(original_func)) (base + ADDRESS);

    OutputDebugString(TEXT("Attach approach!"));
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&(PVOID&)original_func, hookedFunction);

    LONG lError = DetourTransactionCommit();
    if (lError != NO_ERROR) {
        OutputDebugString(TEXT("Attach fail!"));
    } else {
        OutputDebugString(TEXT("Attach Sucess!!"));
    }
    return 0;
}

DWORD WINAPI Detach(LPVOID param)
{
    OutputDebugString(TEXT("Detach approach!"));
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourDetach(&(PVOID&)original_func, hookedFunction);

    LONG lError = DetourTransactionCommit();
    if (lError != NO_ERROR) {
        OutputDebugString(TEXT("Detach fail!"));
    } else {
        OutputDebugString(TEXT("Detach Sucess!!"));
    }
    return 0;
}

BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
    switch(dwReason)
    {
        case DLL_PROCESS_ATTACH:
        {
            CreateThread(0, 0, Attach, hModule, 0, 0); 
            break;
        }
        case DLL_PROCESS_DETACH:
        {
            CreateThread(0, 0, Detach, hModule, 0, 0); 
            break;
        }
    }
    return TRUE;
}
target.cpp程序的控制台输出

0 7
0 7
0 7
0 7
0 7
0 7
0 7 <-- (DLL injection!)
000D1069 Hooked Obj Address.
original function returns 843966720 <-- why not 0 or 7?
000D8B30 Hooked Obj Address.
original function returns 890668
890668 843966720
000D1069 Hooked Obj Address.
original function returns 843966720
000D8B30 Hooked Obj Address.
original function returns 890668
正如您在控制台输出中看到的,原始函数返回垃圾值。 为什么原始函数返回垃圾值?

来自:

为了正确截取,目标函数、迂回函数和目标指针必须具有完全相同的调用签名,包括参数数量和调用约定。使用相同的调用约定可以确保寄存器得到适当保留,并且堆栈在迂回函数和目标函数之间正确对齐

int hookedFunctionvoid*obj与此调用的调用约定不匹配


可能还有其他错误,但这是一个明显的错误。

好的,我解决了这个问题,只是在hookedFunction中添加了调用约定。