Advertisement

What are hooks?

Started by April 30, 2001 02:14 AM
40 comments, last by Merge2 23 years, 9 months ago
I read another post that talked about hooks in programming, and I asked around in some IRC channels, and no one could really explain it. So what is a hook and what is it used for?
A hook is a thing that allows you to tap into the stream of messages being sent to a window. If your a C++ programmer I''m sure you know that when windows wants to tell you something it sends a message to your program saying what it wants you to know. Such as part of your window needs to be repainted or the mouse moved over your window. Well windows sends messages like those to your app right, well a hook allows you to get the messages sent to any window you hook.
Advertisement
if you want more info on this search MSDN (if you dont have it on your computer look at msdn.microsoft.com) just search for SetWindowsHook. dont add spaces to it and you should be able to find some helpful info
I read about a speed cheat that was written for Half-life (specifically the mod counter-strike, but it works on HL and all HL modes). The person commenting on the cheat said that he was an ASM programmer and that all the cheat did was get a hook to the timer or clock or something, and make the game think the computer was running slower than it was, and so the game compensated for it, causing a player to be able to run, shoot, reload, etc., at extreme speeds.

I looked up the SetWindowsHookEx function, and I''m not sure how all of this works. I''m trying to understand how this works in actual use, by this example. Let me see if I get this right. Basically you can get a hook to something, like a message. And everytime that message is sent to the queue of another process, if you have a hook setup, then you will be notified and your function will run. Is this similair to how you had to reprogram the keyboard interupt to get rid of the buffered delay in the old days of DOS? Would that be an example of a hook?
Well the system clock generates an interrupt. That guy could of just changed what happens when the interrupt is called by changing the interrupt in the interrupt vector table, all in ASM.

Now hooks aren''t anything specific like windows messages or anything. Its more of a term than a definition. It basically means a place for you to "tap in" to something thats happening and change things, or add your own stuff. For example, since that guy was able to insert his own code between the real code, he found a "hook", a place where he could add his own stuff, while everything else would continue normally, only executing his code too.
here is a tutorial i wrote:
keyboard hook
check it out :>)

Arkon
[QSoft Systems]
Advertisement
I did mess up in the way I worded what I said. As I said before when windows wants to tell a object (I''m using object as like a window a button or what ever) something it does it by sending a message to the object. What hooking does is allow you to (as I said before and someone quoted me on) tap into the stream of messages being sent to the object. As the people that said stuff after me pretty much told you a hook allows you to get the messages and then you can execute some code of your own if you would like. But the reason I repost is because I don''t really see how the person did this unless the person stoped the messages and sent his own custom ones to it. This can easily be done but I thought I''d just tell you that. As I said lastnight to you on the GameDev IRC channel I''ll help you out with it today. -Kyle
Problem with SetWindowsHookEx is that it is called by a message pump and is thus useless when app is using DirectInput in exclusive mode. What ***I*** would like to see is a kind of VxD that will for example give a call to MyHook.dll which can be customized, thus you can monitor keyboard and build keyboard macros for games that do use Direct Input.
Anyone has something that would work like that?
ok i get it..
well vxd don''t use the hook dlls
you create the hook in a vxd, and it will be better then a .dll because it works at ring 0 that''s why direct input will receive the keys you feed it... or get the keys from direct input

i haven''t tried to write a keyboard vxd yet, i just downloaded the ddk yesterday, i hope it''s easy

but for start, the kb hook can be helpful for key loggers anf stuff...

Arkon
[QSoft Systems]
I was wondering if anyone knows how does MS Strategic Commander (USB hardware game controler-macro thingy) inserts keyboard input practicaly anywhere? I know how to detect keys from exclusive DI app., but how do you insert keys into DI buffer?
Regards, Sasha.

This topic is closed to new replies.

Advertisement