开发者

MS Detours 2.1 - Popping out of stack

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 开发者_JAVA技巧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;
}


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜