[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Why SetWindowsHookEx call failure with a S_OK?

Posted on 2005-03-09
5
Medium Priority
?
598 Views
Last Modified: 2013-11-20
// 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
0
Comment
Question by:Struggler
2 Comments
 
LVL 16

Expert Comment

by:nonubik
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.

0
 
LVL 86

Accepted Solution

by:
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"
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…

612 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