SetWindowsHookEx() in a Windows Service?

    Date: 08/11/05 (C Sharp)    Keywords: programming, software, xml, security, web, google

    Hello all. First time posting -- I'm an executive/part owner of a small, 30-40 employee software development company in the US. Can't get any more specific, due to LJ drama that occurred in the past, from which I learned that my constant insistence upon plausible deniability is never a bad thing.

    Anyway, all of our current contracts are in .NET, from winforms applications, to windows services, to webservices, to web applications. We write in C#.NET and VB.NET depending on the contract, application, and developer. We've been working with .NET for the past 2-3 years.

    I don't do as much programming with my company as I did when I first started there as Lead Developer; a good deal of my responsibilities have changed to software design/architecture, scheduling, project management, and having my time wasted by as many meetings as possible, at the most inconvenient times.

    So perhaps these latest developments will lend an excuse to the following ignorance. :)

    I am writing an application for myself, completely unrelated to my company, in my "free time." It has a fairly eccentric variety of functionality, all of which is related in a certain way, but the functionality with which this post concerns itself is keypress logging.

    I've written a class that uses the SetWindowsHookEx() WinAPI call to install a hook for keyboard events (using WH_KEYBOARD_LL). It sets up the hook, which simply raises an event (passing the keypress data along) before giving the keypress back to the system (which continues down the chain of installed hooks). I've written a parent class which attaches a method to the event that takes the useful information from the keypress data and stores it in a queue. The remainder of this parent class provides access to the queue, functionality to clear it, etc. The class that implements SetWindowsHookEx() is in a DLL (I wrote it as part of a separate application back when I was more comfortable with VB.NET, while the current project is in C#, thus the DLL), and the parent class (in C#) imports this DLL.

    The class performs flawlessly in the Windows Forms application I wrote to test all of the classes destined to be part of the final solution. It properly logs all of the keypresses in its queue, its methods provide the proper access to the queue, and the test application could write the keypresses to a file, store them in an XML object, or whatever needed to happen. I was pleased, since the whole mess only took about 2 hours to develop and test.

    However, the final application that will be doing the keylogging is a Windows Service. Now that I finished developing and testing all the classes I needed to use in the final application, I've begun building the service. I've got the service properly installing, starting, and logging to the system's event log, so I know the service itself is working. However, the keylogging class is not working. No exceptions are thrown, and the WinAPI call (from user32.dll) returns no error codes. However, the queue count remains at 0, and no keys are logged at all.

    I've simply dragged the class from the test Winforms application into the Windows Service application, and used it in the same way. While keypresses were logged efforltessly in the Winforms application, not a single one is being logged in the Windows Service.

    I have tried pretty much everything I can think of, from checking the "Allow this service to interact with the desktop" checkbox, to changing the account running the service (currently the Local System account). However, as I said above, programming is not a day-to-day thing for me anymore, so I am most likely ignorant of something here.

    I've read many accounts via Google of people successfully using SetWindowsHookEx() in a Windows Service, so I know it can be done. What gives? Is this a permissions/security issue, and if so, why are no errors/exceptions being thrown? Do I need to register the DLL containing the class that calls SetWindowsHookEx() in the GAC for any reason?

    Any information or help will be greatly appreciated. Thanks in advance...!

    Source: http://www.livejournal.com/community/csharp/32613.html

« Phat C# Database Tool || hmm »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home