MS Detours 2.1 - 从堆栈中弹出

发布于 2024-11-13 03:12:27 字数 1497 浏览 2 评论 0原文

我不会绕过扫雷中的 PlaySoundW 函数。 游戏一调用 PlaySoundW 函数就崩溃。 如果我在代码中取消注释 Beep,游戏会发出蜂鸣声然后崩溃。

现在代码正在从挂钩函数调用原始函数,因此它不应该执行任何操作。但无论如何它都会崩溃。

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

在 Olly 中调试应用程序后,我发现当 detour 处于活动状态时,并非所有垃圾都会从堆栈中弹出。 如何修复它?

这是我的代码:

#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;
}

I wont to detour PlaySoundW function inside Minesweeper.
Game is crashing as soon as it calls PlaySoundW function.
If I uncomment Beep inside my code, game beeps and than crashes.

Now code is calling original function from hooked function so it should't do anything. But it is crashing anyway.

Can you tell me what is wrong?

After debugging app in Olly I found that when detour is active not all rubbish is popped out of stack.
How to fix it?

This is my code:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2024-11-20 03:12:27

尝试将 HookPlaySoundW 的调用约定设置为 __stdcall (因为 PlaySoundW 的 CC 也是 __stdcall (来自 >Windows.h): WINMMAPI BOOL WINAPI PlaySoundW( __in_opt LPCWSTR pszSound, __in_opt HMODULE hmod, __in DWORD fdwSound);)。

我工作前后都走了不少弯路,随便一瞥,除了我上面提到的以外,一切看起来都是正确的。如果这不能解决您的问题,我很乐意做一些进一步的调查。

Visual C++ 的默认设置是 __cdecl,其中调用*er* 会清理堆栈,但在 __stdcall 中,调用* ee* 清理堆栈。这可能是(ie可能是)所有“垃圾从堆栈中弹出”的原因。

Try setting the calling convention of HookPlaySoundW to __stdcall (because the CC of PlaySoundW is also __stdcall (from Windows.h): WINMMAPI BOOL WINAPI PlaySoundW( __in_opt LPCWSTR pszSound, __in_opt HMODULE hmod, __in DWORD fdwSound);).

I have worked with detours before and after a casual glance everything looks correct except what I mentioned above. If this doesn't resolve your problem I'd be glad to do some further investigation.

The default setting for Visual C++ is __cdecl in which the call*er* cleans up the stack, but in __stdcall the call*ee* cleans up the stack. This is probably (i.e. might possibly be) the reason for all the "rubbish being popped off the stack".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文