0

我有以下问题:我有一个旧的 DetourFunction 这些工作正常..现在想使用新的 DetourAttach 但我的 Hook 不再工作了......也许有人知道我做错了什么。

老一:

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

DWORD score_adr = 0x01013C89;
typedef DWORD *(__stdcall *score)(DWORD *a1, int a2);
score o_score;

DWORD *__stdcall h_score(DWORD *a1, int a2)
{
    static int new_score;
    new_score += 1;
    a1[1] = 1;
    return o_score(a1, new_score);
}

BOOL __stdcall DllMain(HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpcReserved)
{
    switch (fdwReason){
    case DLL_PROCESS_ATTACH:
        o_score = (score)DetourFunction((PBYTE)score_adr, (PBYTE)&h_score);
        break;}
    return TRUE;
}

新的一个:

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

DWORD score_adr = 0x01013C89;
typedef DWORD* (__stdcall* o_score)(DWORD* a1, int a2);
o_score score;

DWORD* __stdcall h_score(DWORD* a1, int a2)
{
    static int new_score;
    new_score += 1;
    a1[1] = 1;
    return score(a1, new_score);
}


BOOL __stdcall DllMain(HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpcReserved)
{
    switch (fdwReason){
    case DLL_PROCESS_ATTACH:

        score = (o_score)(score_adr);
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread);
        DetourAttach((PVOID*)(&score), (PVOID)h_score);
        DetourTransactionCommit();
    }
    return TRUE;
}
4

1 回答 1

0

它现在工作...

#include <cstdio>
#include <windows.h>
#include <detours.h>

typedef void (WINAPI *score)(DWORD* a1, int a2);
void WINAPI h_score(DWORD* a1, int a2);

score Nscore = (score)(0x01013C89); //Set it at address to detour in
                    //the process
INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
    switch(Reason)
    {
    case DLL_PROCESS_ATTACH:
        {
            DisableThreadLibraryCalls(hDLL);
            DetourTransactionBegin();
            DetourUpdateThread(GetCurrentThread());
            DetourAttach(&(PVOID&)Nscore, h_score);
            DetourTransactionCommit();
        }
        break;
    case DLL_PROCESS_DETACH:
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)Nscore, h_score);
        DetourTransactionCommit();
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
        break;
    }
    return TRUE;
}

void WINAPI h_score(DWORD* a1, int a2)
{
    static int new_score;
    new_score += 1;
    a1[1] = 1;
    return Nscore(a1, new_score);
}
于 2019-12-02T11:32:51.340 回答