I read you article with interest. I have been working with hooks since 1999 but have never really understood how they actually functioned. Thank you for the fine elucidation. I have yet to try anything in C# but then will soon different.
I have been scrounging the net for scraps of information related to what I informally call post-hooking. The idea being to trap an event, say a mouseclick, AFTER the hooked application has received it and before Windows throws it away. I have found nothing to indicate that this is possible and that is at least encouraging.... i.e. no has said it isn't possible.
I don't know whether my question is related to your article or not, but just wanted to ask you this (since I feel it is related!!)
I am having my .net application having so many classes/methods. Is there anyway that I can have a global hook kind of thing so that whenever a method is called in some class, a trace containing the method name and its parameters can be written to a txt file?
I'm adding some code to your hooksource (and I will post it there when finished)
But I have one problem, I know the C# so I can write new things in C++ kinda easy but what should I return instead of CallNextHookEx(hookMouse, code, wparam, lparam); ???? (Checked it and it must be an LRESULT)
That's cool. It's neat that people are making use of that code!
I'm not sure what to use instead of LRESULT off the top of my head. But you can find out usually. In some C++ code type LRESULT, then right-click on it and say go to definition. That will usually give you something like:
#define LRESULT long
or something like that (it might not be long and it might take a few recusived steps to resolve). Does that help?
You can help me? I'm working in a WINDOWS CE project with Visual Studio 2003 .net, and I want to use a global hooks application with a project in C# for my pocket PC device under windows ce .net. I ask you if it's possible to do this? In other words, I ask you if it's possible to build an application that works under Pocket PC?
These examples that are in the other messages, work under pocket pc for global hooks??? Your application works in Pocket PC?
m_pfSetWindowsJournalHook is a pointer to the QASetWindowsJournalHook function, that's in the coredll.dll library.
I have a JournalRecordProc process, and finally I want to delete the global hook:
where m_pfUnhookWindowsJournalHook is a pointer to the QAUnhookWindowsJournalHook function, that's in the coredll.dll library.
And my problem, is that this function returns false, instead of true, and that wants to say that the global hook isn't deleted correctly, and the system resources are not free. In addition, when my application finishes in the Pocket PC, the PDA becomes blocked, and it doesn't detect any other keyboard or mouse event, and I have to do a software reset.
There is a function, called GetLastError(), that gives you the last error that happens in the system, but this function returns 0, that wants to say, "all is correct". Someone can help me, and knows how delete this global hook correctly?
HINSTANCE aInstance = GetModuleHandle(NULL); --> aInstance makes reference to real module, not to a dll, it's not necessary! (it can be also an EXE file);
I have a problem. I'm using a WH_KEYBOARD_LL hook in order to capture all system keyboard events. I create the hook without problems, but when I want to delete it, the function unhookWindowsHookEx() returns a FALSE (that wants to say error in the function), but the hook is correctly deleted, because if after I create another WK_KEYBOARD_LL hook, it will be created correctly.
But my main problem is that in the LowLevelKeyboardProc (the hook process), I cannot detect all keyboard events of all the applications of my pda pocket pc. I only detect the message WM_KEYDOWN in a few number of keys such as: caps lock, shift, esc, and so on. And I want to detect all keyboard events. What do you think that could be the problem?
I ask for your help, please. It's very important for me.
Here you have the declaration of the LowLevelKeyboardProc:
im fairly new to .net and c#, but i have successfully used your hooking code in a console application.
however, i need to do something similar with a system service. i tries using your code, but i get no output form the service. so, i have 3 simple questions: 1) is it possible to achieve this with your code? 2) if so, then how? some clue/pointer would be very helpfull. 3) if not, than do you know of some other project which can help me?
I'm having a hard time ferreting out exactly how to insert a windows hook that will allow me to monitor messages to a window outside my own application. I don't need a global hook - I know the handles to the specific windows I want to watch - but I believe I'll still suffer all the complications of having to inject a DLL into a different application's memory space. I'm trying to monitor window placement information, so I want to capture creation, destruction, and movement.
Here's the context:
I've written a free add-on tool in C# for poker players on Party Poker. My tool is window-management software which positions and re-sizes their poker tables. The tool snaps tables into user-defined "slots" and allows users to swap two tables by dragging one over the other. When playing multiple tables, managing the windows is a chore, so I wanted to automate it. That's what computers are for!
The tool is fully functional and several hundred players are using the Beta, but I'm trying to make the architecture more elegant by monitoring windows messages. You can check out the tool and download it to try here: http://www.overcards.com/wiki/moin.cgi/PartyPlanner
It requires Party Poker, of course; if you don't want to install Party Poker, you can run a Mock application I wrote for testing. You can download it here: ftp://www.overcards.com/downloads/MockParty.zip
In the current architecture, I have a loop on a timer uses Windows API calls to get all top-level windows, identify poker tables, get their positions and states, etc. Then my code uses more calls to position the tables according to plan. I also give the user a drag handle on top of their table by creating a transparent form which overlays the poker table; dragging the handle moves the poker table. To keep the drag handle visible, I also need to insert my form before the table in the z-order. As you can see, all this works (although there's a bit of delay on the z-order positioning after you click on a table; I initially used TopMost, but that was awkward when the user positions a browser or other window in front of the table, and the drag handle pokes through!)
What I want to do is get away from the timer loop entirely. I would like to put out a windows hook into each running poker table and watch the sizing and movement messages. This requires throwing a local windows hook into a foreign windows application and, as I said before, I think this suffers all the limitations of global hooks.
Though I have a degree in computer science, I never worked as a programmer, so my skills are rusty. . . after many years, I became a poker player, and now I've picked up the coding as a hobby. I've learned C# and VB.NET; my pointer referencing/derefrencing skills have rusted shut, and I'm out of my element when writing the DLL component of this solution!
If I'm wrong about the approach/difficulty, I'd love to be told it's easier than I think. I'd greatly appreciate any advice you could give, or any useful references you could point me to.
If you think posting this message to the public forums here would be fruitful, I'll give that a try, too.
Thanks in advance for your great work, for reading my long post, and for any time you spend in reply!
By the way -- the overall technique also allows one to extend an application even if it doesn't lend itself to third-party extensions. Using the same techniques, I can add a control which positions itself in front of a target window. From the user's perspective, you've added a control TO the other application. I think it's a useful technique. If others agree, I may write up an article describing the process.
First off, let me tell you that we have grown quite fond of these libraries and they have helped us tremendously. I thank you for your hard work on this code.
I am hoping you will be willing to provide me with some help, advice, anything. We were using your libraries in a C# .NET v1.1 application. We have since upgraded this application to the newer 2.0 (2005) version of .NET. We have user requirements that we must deploy our application using One-Click deployment. I am trying to set up our project so I can include the SystemHookCore.dll when we Publish our application. Since it is an unmanaged C++ dll, I am unable to add a reference to it, and thus unable to Publish it with our application.
So I’ve gone to the effort of porting your code to .NET 2.0 and rebuilding the SystemHookCore.dll as a managed dll so that I can include a reference to it. I had to change some compiler/linker settings to do this.
When I first started my application I was getting a MDA error with this message.
LoaderLock was detected Message: DLL 'Q:\MAX\Crossfire\ClientApp\bin\Debug\SystemHookCore.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
The offending line of code was in the constructor of the SystemHook class. Here’s the code:
SetCallBackResults result = SetUserHookCallback(_processHandler, _type);
So after some research I changed the linker setting to /noentry. This allowed me to get past this MDA LoaderLock error. However, now when I try to hook up a key listener it fails to initialize. The DllMain code is never executed so the g_appInstance is null and thus when I execute this code:
keyboardHook = new KeyboardHookExt(); keyboardHook.InstallHook();
The install hook throws and exception when the InstallHook method in SystemHook base class calls InitializeHook. When I debug it the g_appInstance is null and it returns false.
I’m apologize for the lengthy post, I figure that if you are willing to help me, the more information I gave you the better.
Again, we really like what you have done here are we would really not want to re-invent the wheel if we don’t have to. Your libraries have worked great for us and I would be grateful if you may find some time to offer me some advice on how to resolve this issue. I can be reached at this email address or you can post a response here and I will check back with you.
Hello Mr.Michael... Im a college student doing a project to make the web browser IE talk. Right now, with the basic req done., im looking at adding keyboard shortcuts(hooks, i presume) to the tool i have written so as to play, pause and other speaking features. How do i go abt doing this? I have coded everythin in C#. I have no idea abt VC++.. So kindly help,sir.. I have seen ur enthusiasm in replying to the fellow members.. Awaiting for ur kind support too...
-- modified at 11:33 Sunday 2nd April, 2006
Last Visit: 31-Dec-99 19:00 Last Update: 23-Jan-17 7:23