Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use SetWindowsHookEx

62 views
Skip to first unread message

James Duy Trinh

unread,
Aug 16, 2008, 1:17:16 PM8/16/08
to
Hi all,

I wrote a mouse hook function on app, but it only work when our program was
activing, if i focused on another app, then my hook function didn't work
more.
Any problem? I must write this function in a dll? So it can work on whole
system?


James Duy Trinh

unread,
Aug 16, 2008, 1:21:37 PM8/16/08
to
My code as below, it is right?

BOOL CMouseLightDlg::StartMouseHook()
{
DWORD dwHookType;
HOOKPROC hProc;

dwHookType = WH_MOUSE;
hProc = (HOOKPROC) MouseProc;

g_hhMouseHook = SetWindowsHookEx(dwHookType,
hProc,
AfxGetApp()->m_hInstance,
0 );

if( g_hhMouseHook == NULL || GetLastError() != 0 )
return FALSE;
return TRUE;
}


"James Duy Trinh" <viet...@gmail.com> wrote in message
news:u2EhwP8$IHA....@TK2MSFTNGP04.phx.gbl...

david

unread,
Aug 16, 2008, 1:56:16 PM8/16/08
to
James Duy Trinh wrote:

Of course.
Read MSDN : everything is explained, with complete samples

David Ching

unread,
Aug 16, 2008, 7:22:10 PM8/16/08
to
"James Duy Trinh" <viet...@gmail.com> wrote in message
news:erxYMS8$IHA....@TK2MSFTNGP05.phx.gbl...

> g_hhMouseHook = SetWindowsHookEx(dwHookType,
> hProc,
> AfxGetApp()->m_hInstance,
> 0 );
>

Your hook needs to be in a DLL, which will be loaded into all processes.
Otherwise the hook will only work for your process.

g_hhMouseHook needs to be declared in shared data segment so that your hook
proc can access it no matter which process it is in.

-- David


James Duy Trinh

unread,
Aug 16, 2008, 9:07:34 PM8/16/08
to
i declared g_hhMouseHook in shared data segment.

"David Ching" <d...@remove-this.dcsoft.com> wrote in message
news:GgJpk.34688$co7....@nlpi066.nbdc.sbc.com...

James Duy Trinh

unread,
Aug 16, 2008, 9:19:58 PM8/16/08
to
i have seen an application, it can monitor Mouse actions without using DLL.

"david" <da...@nospam.com> wrote in message
news:ujpagl8$IHA....@TK2MSFTNGP05.phx.gbl...

David Ching

unread,
Aug 16, 2008, 10:04:39 PM8/16/08
to
"James Duy Trinh" <viet...@gmail.com> wrote in message
news:%23UhefdA...@TK2MSFTNGP03.phx.gbl...

>i have seen an application, it can monitor Mouse actions without using DLL.
>

Some hooks don't need to be in a DL, like WH_MOUSE_LL. But WH_MOUSE does
need to be in a DLL.

-- David


Joseph M. Newcomer

unread,
Aug 16, 2008, 10:10:05 PM8/16/08
to
See my essay on hooks on my MVP Tips site. Your problem almost certainly is that you have
not put your hook function in a DLL, and therefore it cannot possibly run except in your
process.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

James Duy Trinh

unread,
Aug 17, 2008, 3:36:50 AM8/17/08
to
I used a dll to hook Mouse actions. It is ok now.

But i have a problem how to use get Delta info of WM_MOUSEWHEEL

In LRESULT CALLBACK HookProc(int ncode, WPARAM wparam, LPARAM lparam)

wparam is equal to MOUSEWHEEL

lparam can't get delta info.

Can you help me?

"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:k22fa4hqjc5l9hguj...@4ax.com...

Joseph M. Newcomer

unread,
Aug 20, 2008, 9:51:09 PM8/20/08
to
What hook are you using?

If WH_GETMESSAGE, then the LPARAM is actually an (LPARAM)(LPMSG) and the LPMSG is the same
LPMSG that would appear for the GetMessage call, so
LPMSG msg = (LPMSG)lParam;
delta = HIWORD(msg->wParam);
should give you the delta.
joe

0 new messages