Problems with Message hooking 
Author Message
 Problems with Message hooking

Hi All,
Im hoping some of you may have a few pearls of wisdom to assist me with a
problem

I am currently writing a DLL for one of our applications. This DLL requires
me to capture messages. In order to do this I have created a CWnd derived
class (class CHiddenWindow: public CWnd). In this class I implement a
WindowProc and ON_MESSAGE method to capture the messages.

I now need to move this into another class within the DLL. i have tried to
use a Window Hook via SetWindowHookEx. However, I am unable to make this
work.

The window I want to hook is defined as

class CHiddenWindow: public CWnd
{
public:
    CHiddenWindow();
    virtual ~CHiddenWindow();
    HWND CreateHiddenWindow();
protected:
    HWND pParent;
    LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
    DECLARE_MESSAGE_MAP ();

Quote:
};

HWND CHiddenWindow::CreateHiddenWindow()
{
    if
(CreateEx(NULL,0,"ICPUHiddenWindow",WS_DISABLED,0,0,0,0,NULL,NULL,NULL))
    {
        return GetSafeHwnd();
    }
    AfxMessageBox("HIDDEN WINDOW CREATION FAILED");
    return (HWND)NULL;

Quote:
}

I then have class that I hook from using

HHOOK hHook;
hHook = SetWindowsHookEx(WH_CALLWNDPROC,HandleMessages,NULL,0);
if (hHook==NULL){
    SendLogDebug(MSG_LOG,"[ERROR %i] Message hook failed",GetLastError());
    return RERROR_WINDOW_CREATION_FAILED;

Quote:
}

I know that I have not passed the correct parameters to SetWindowHookEx.
Where I pass NULL I should  pass a handle to the DLL containing the hook
procedure pointed to by the lpfn parameter.

If I pass NULL I get a error 1428 Cannot set nonlocal hook without a module
handle.

Im wondering if someone out there has some example code or advise that may
help me ASAP ?



Wed, 30 Nov 2005 22:36:05 GMT  
 Problems with Message hooking
Have a look at MFC source code. MFC uses message hooks.

Ivan


Quote:
> Hi All,
> Im hoping some of you may have a few pearls of wisdom to assist me with a
> problem

> I am currently writing a DLL for one of our applications. This DLL
requires
> me to capture messages. In order to do this I have created a CWnd derived
> class (class CHiddenWindow: public CWnd). In this class I implement a
> WindowProc and ON_MESSAGE method to capture the messages.

> I now need to move this into another class within the DLL. i have tried to
> use a Window Hook via SetWindowHookEx. However, I am unable to make this
> work.

> The window I want to hook is defined as

> class CHiddenWindow: public CWnd
> {
> public:
>     CHiddenWindow();
>     virtual ~CHiddenWindow();
>     HWND CreateHiddenWindow();
> protected:
>     HWND pParent;
>     LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
>     DECLARE_MESSAGE_MAP ();
> };

> HWND CHiddenWindow::CreateHiddenWindow()
> {
>     if
> (CreateEx(NULL,0,"ICPUHiddenWindow",WS_DISABLED,0,0,0,0,NULL,NULL,NULL))
>     {
>         return GetSafeHwnd();
>     }
>     AfxMessageBox("HIDDEN WINDOW CREATION FAILED");
>     return (HWND)NULL;
> }

> I then have class that I hook from using

> HHOOK hHook;
> hHook = SetWindowsHookEx(WH_CALLWNDPROC,HandleMessages,NULL,0);
> if (hHook==NULL){
>     SendLogDebug(MSG_LOG,"[ERROR %i] Message hook failed",GetLastError());
>     return RERROR_WINDOW_CREATION_FAILED;
> }

> I know that I have not passed the correct parameters to SetWindowHookEx.
> Where I pass NULL I should  pass a handle to the DLL containing the hook
> procedure pointed to by the lpfn parameter.

> If I pass NULL I get a error 1428 Cannot set nonlocal hook without a
module
> handle.

> Im wondering if someone out there has some example code or advise that may
> help me ASAP ?



Thu, 01 Dec 2005 13:02:21 GMT  
 Problems with Message hooking
On Sat, 14 Jun 2003 23:36:05 +0200, "Marc Teichtahl"

Quote:

>If I pass NULL I get a error 1428 Cannot set nonlocal hook without a module
>handle.

You've told it you want to set a global hook, i.e. the last param to
SetWindowsHookEx is zero :

SetWindowsHookEx(WH_CALLWNDPROC,HandleMessages,NULL,0);

From what you said earlier ("The window I want to hook is defined
as...") this is not the case. The OS is telling you that nonlocal
(i.e. global) hooks can't be set in this manner. Either you have a
thread ID (local) or you have a HMODULE (global).

You do realise the hookproc is a callback and therefore can't be a
class member function, don't you ?



Fri, 02 Dec 2005 11:18:50 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Q: subclassing problem, with hook and windows messages

2. Windows Message Hooks Problems

3. System wide Message Hook not working when caller app loses focus

4. Hooking in C# up to a DLL generated messages

5. message hooks

6. dll to hook messages

7. Want to know Message Hooking

8. Message Hooking of JournalRecord/Play

9. hook WM_SYSCOMMAND message

10. Journaling using Windows Message Hooks

11. hook WM_INITMENUPOPUP message

12. dll to hook messages

 

 
Powered by phpBB® Forum Software