Why SetWindowsHookEx call failure with a S_OK?

Posted on 2005-03-09
Medium Priority
Last Modified: 2013-11-20
// Returns the HMODULE that contains the specified memory address
static HMODULE ModuleFromAddress(PVOID pv) {
    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);
    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
Question by:Struggler
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Expert Comment

ID: 13493996
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.

LVL 86

Accepted Solution

jkr earned 200 total points
ID: 13496452
You can't specify both a module handle *and* a thread ID - see the docs on 'SetWindowsHookEx()':

"The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process"

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question