SetWindowsHookEx not working with x64 OS

Hello
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!!
ICSAutomationAsked:
Who is Participating?
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)?
0
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(
                    WH_MOUSE_LL,
                    MouseHookProcedure,
                    Marshal.GetHINSTANCE(
                        Assembly.GetExecutingAssembly().GetModules()[0]),
                    0);
****************
- 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...
0
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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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.
http://bit.ly/1AwGSbj
0
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.
06-03-2015-15-25-15.bmp
0
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, 
	MouseHookProcedure, 
	(IntPtr)0,
	AppDomain.GetCurrentThreadId());

Open in new window



See also this example:

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

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:
09-03-2015-10-00-28.bmp
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 :-(
0
ICSAutomationAuthor Commented:
This was not the final solution, but I got some good inputs. Thanks!!
I finally found a solution, without using "SetWindowsHookEx"
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.