MS Detours 2.1 - 弹出堆栈

时间:2011-06-05 20:26:41

标签: c++ winapi code-injection detours

我不会绕过扫雷中的PlaySoundW功能。 一旦调用PlaySoundW功能,游戏就会崩溃。 如果我在代码中取消注释Beep,游戏会发出哔哔声而不是崩溃。

现在代码从钩子函数调用原始函数,所以它不应该做任何事情。但无论如何它都在崩溃。

你能告诉我出了什么问题吗?

在Olly调试应用程序后,我发现当绕行时,并非所有垃圾都会从堆栈中弹出。 如何解决?

这是我的代码:

#include <Windows.h>
#include <tchar.h>
#include <detours.h>

namespace Hooks
{
    BOOL(__stdcall *OrgPlaySoundW)(LPCTSTR pszSound, HMODULE hmod, DWORD fdwSound) = &PlaySoundW;

    BOOL HookPlaySoundW(LPCTSTR pszSound, HMODULE hmod, DWORD fdwSound)
    {
        //Beep(1000, 250);
        //return TRUE;
        return OrgPlaySoundW(pszSound, hmod, fdwSound);
    }

    void DetourPlaySoundW(BOOL disable)
    {
        if(!disable)
        {
            DetourTransactionBegin();
            DetourUpdateThread(GetCurrentThread());
            DetourAttach(&(PVOID&)OrgPlaySoundW, &HookPlaySoundW);
            DetourTransactionCommit();
        } else 
        {
            DetourTransactionBegin();
            DetourUpdateThread(GetCurrentThread());
            DetourDetach(&(PVOID&)OrgPlaySoundW, &HookPlaySoundW);
            DetourTransactionCommit();
        }
    }
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch(fdwReason)
    {
    case DLL_PROCESS_ATTACH:
        Hooks::DetourPlaySoundW(FALSE);
        break;
    case DLL_PROCESS_DETACH:
        Hooks::DetourPlaySoundW(TRUE);
        break;
    }
    return TRUE;
}

1 个答案:

答案 0 :(得分:2)

尝试将HookPlaySoundW的调用约定设置为__stdcall(因为PlaySoundW的CC也是__stdcall(来自Windows.h):{{1} })。

除了上面提到的以外,我偶然看了一眼看上去都很正常。如果这不能解决您的问题,我很乐意进行进一步的调查。

Visual C ++的默认设置为WINMMAPI BOOL WINAPI PlaySoundW( __in_opt LPCWSTR pszSound, __in_opt HMODULE hmod, __in DWORD fdwSound);,其中调用* er *清理堆栈,但在__cdecl调用* ee *清理堆栈。这可能是(可能是)所有“垃圾从堆栈中弹出”的原因。

相关问题