Link to home
Start Free TrialLog in
Avatar of Struggler
Struggler

asked on

Why SetWindowsHookEx call failure with a S_OK?

// Returns the HMODULE that contains the specified memory address
static HMODULE ModuleFromAddress(PVOID pv) {
    MEMORY_BASIC_INFORMATION mbi;
    return((VirtualQuery(pv, &mbi, sizeof(mbi)) != 0)
        ? (HMODULE) mbi.AllocationBase : NULL);
}

IHOOKAPI bool InstallHook(DWORD dwThreadId, LPCSTR pszSection, LPCSTR pszTaskFile, bool fInstall)
{
    if (fInstall)
    {
        ...
        g_hHook = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, ModuleFromAddress(CallWndProc), dwThreadId);
        ASSERT(g_hHook);
    }
    else
    {
        UnhookWindowsHookEx(g_hHook);
    }
    return g_hHook != NULL;
}

these codes resides in a dll, and i call installhook from main exe. It fails at ASSERT(g_hHook), and @err,hr shows S_ok
Avatar of nonubik
nonubik

SetWindowsHookEx does not return a HRESULT, but a HHOOK (a handle, pointer). When it fails it returns NULL. NULL is defined as 0, same as S_OK. So you are misslead by the err,hr.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial