gpt4 book ai didi

c++ - MS Detours 2.1 - 从堆栈中弹出

转载 作者:行者123 更新时间:2023-11-30 04:34:03 25 4
gpt4 key购买 nike

我不会在 Minesweeper 中绕过 PlaySoundW 功能。游戏一调用 PlaySoundW 函数就崩溃了。如果我在我的代码中取消对 Beep 的注释,游戏会发出哔哔声然后崩溃。

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

你能告诉我哪里出了问题吗?

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

最佳答案

尝试将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* 清理堆栈。这可能是(可能是)所有“垃圾从堆栈中弹出”的原因。

关于c++ - MS Detours 2.1 - 从堆栈中弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6245639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com