SetWindowsHookEx not working with x64 OS

I had a C# application on VS2003, running on XP and WinServer2003 (both x86 OS).
In this application I use the hook "SetWindowsHookEx" to detect user activities and make a auto logout.

I migrated the application to VS2010 (.Net4.0) which I run on WinServer2008R2 and Win7prof, both running on x64 machines. The target platform of the application is still x86.

Unfortunately, the "SetWindowsHookEx" is not working any more :-(

What could be the reason?

Thanks for any help!!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
So, you're getting NULL as result. And what error code do you get (GetLastError)?
ICSAutomationAuthor Commented:
Sorry, I should have explaned a bit more exactly...
What I analysed so far:
- the Hook could be set:
************  Code *********
                // Create an instance of HookProc.
                MouseHookProcedure = new HookProc(MouseHookProc);
                //install hook
                hMouseHook = SetWindowsHookEx(
- the procedure "MouseHookProc" is called the first time I mouve the mouse
- but after the hook called "MouseHookProc" once, it is not called any more.....
- I don't gett any exception or error code...
ste5anSenior DeveloperCommented:
Have you checked the value in hMouseHook after the function call?

Also, where does Assembly.GetExecutingAssembly().GetModules()[0] point to? I'm quite sure that this does not return the correct thread id. See SetWindowsHookEx function.  I would expect it to be zero or the thread ID of your UI thread.

btw, in .NET I would try to solve this with an IMessageFilter.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

David Johnson, CD, MVPOwnerCommented:
To hook all applications on the desktop of a 64-bit Windows installation, install a 32-bit global hook and a 64-bit global hook, each from appropriate processes, and be sure to keep pumping messages in the hooking application to avoid blocking normal functioning. If you already have a 32-bit global hooking application and it doesn't need to run in each application's context, you may not need to create a 64-bit version.
ICSAutomationAuthor Commented:
Sorry, I was out of the office for a few days...

>>>> Have you checked the value in hMouseHook after the function call?
 Yes, hMouseHook  is 197329197 ( a different nuber after every start). Could this be the thread ID?

>>>> Also, where does Assembly.GetExecutingAssembly().GetModules()[0] point to?
It points to my applicaiton, but I don't see a thread Id in the modul data (see picture)

If I call later "CallNextHookEx" it returns my the value 0.

>>>> btw, in .NET I would try to solve this with an IMessageFilter.
I cannot use IMessageFilter because I want to get the event, also if the focus is not on my application.
ste5anSenior DeveloperCommented:
Forget the IMessageFilter, this was nonsense. But I was right about the thread ID. You need the ID of your thread from the app domain:

hHook = SetWindowsHookEx(WH_MOUSE, 

Open in new window

See also this example:

How to set a Windows hook in Visual C# .NET

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ICSAutomationAuthor Commented:
Thanks for your answer!!

I need to install a low-level mouse input (WH_MOUSE_LL) which is also activated if the focus is not on my application.
(the example shows only mouse monitoring in the application)

I implemented it like this:
It was working fine in my old x32 Windows Server 2003 and the old WinXP environement. (two devices)
But since I replaced the hardware to a virtual x64 Windows Server and virtual Windows7 envirtonements, the hook does not work properly any more :-(
ICSAutomationAuthor Commented:
This was not the final solution, but I got some good inputs. Thanks!!
I finally found a solution, without using "SetWindowsHookEx"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.