Important alert: (current site time 7/16/2013 2:55:50 AM EDT)
 

winzip icon

WinSubHook2 Thunks - updated 2/13

Email
Submitted on: 2/1/2004 6:05:41 AM
By: Paul Caton 
Level: Advanced
User Rating: By 41 Users
Compatibility: VB 5.0, VB 6.0
Views: 31320
 
     Version 2 of my funky thunks. Teaser: check out the global keyboard hook sample as the basis of a spy app... with active app tracking. Update 2/13 minor enhancement to cTimer.cls

 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/1/2004 6:24:05 AMPaul Caton

What's new? First, the interface has been coalesced into a single callback with an extra parameter. If you think about it, the only time you need to know is if you're subclassing a message both before and after the original WndProc. Next...
(If this comment was disrespectful, please report it.)

 
2/1/2004 6:25:35 AMPaul Caton

Callback gating has been superceeded with global breakpoint and stop detection... courtesy of Vlad. PBUH. Next...
(If this comment was disrespectful, please report it.)

 
2/1/2004 6:28:13 AMPaul Caton

Thunks now reside in allocated memory. I did find one way of crashing v1 in the IDE. Using the cHook.cls in a tiny project and hitting the end button. Now fixed, I haven't seen a crash in months of testing. Next...
(If this comment was disrespectful, please report it.)

 
2/1/2004 6:30:12 AMPaul Caton

All the assembler has been recast for speed and size. The asm directory includes a ReadMe that explains some of my cunning stunts... for those that are interested. Also included, the tool I use to extract machine code bytes from exe's, dll's and .bin's. Next...
(If this comment was disrespectful, please report it.)

 
2/1/2004 6:33:27 AMPaul Caton

All of the samples have been re-written.. so it's worth another look. New samples include system-wide keyboard and mouse samples. Finally, I've included a sample Visual C++ dll source for those global hooks that you just can't do in VB... yet... maybe :-) HTH, enjoy.
(If this comment was disrespectful, please report it.)

 
2/1/2004 9:54:37 AM

Tremendous work, Mr. Paul
'Ecellent' from me. Your work goes like a wizard. I will definitely like to learn something from you (i ve been interested in system programming for quite a while, but vb sux :) ). Drop me a word if u like.
(If this comment was disrespectful, please report it.)

 
2/1/2004 11:16:54 AMPhantom Man

Well Done Paul.
Very Nice Update.

(If this comment was disrespectful, please report it.)

 
2/1/2004 1:08:05 PM

Very advanced stuff ...
Most of it beyond me but it works !!!
(If this comment was disrespectful, please report it.)

 
2/1/2004 8:25:34 PMCodeClub

crazy!!! 5 balls
(If this comment was disrespectful, please report it.)

 
2/1/2004 8:49:25 PMgonchuki_

Excellent as always!
i will be updating my code to reflect the changes...
(If this comment was disrespectful, please report it.)

 
2/2/2004 12:15:22 AMThushan Fernando

Paul, thanks again for this very impresive submission. I implemented your code in my work as well so i'll be updating that at a later stage with your new submission:)
(If this comment was disrespectful, please report it.)

 
2/4/2004 12:03:25 AMAlexandru Ionescu

Great job again Paul =)
(If this comment was disrespectful, please report it.)

 
2/5/2004 12:48:04 PMLuke H.

holy crap! I don't think that the designers of VB wanted people to be able to do this! Who didn't give this five stars, anyway? Best submission ever to PSC.
(If this comment was disrespectful, please report it.)

 
2/6/2004 7:39:44 PMPaul Caton

Small update: A file was missing from the HookMouse sample. eeeks!

Thanks for all the votes. I'll make some replies over the weekend.
(If this comment was disrespectful, please report it.)

 
2/9/2004 5:08:08 PMLuke H.

I hate to ask, seeing the extraordinary amount of work that was obviously put into it, but... What would it take to associate a long value with a timer? For example, if you wanted a usercontrol with two timers, it'd be really nice to have a tag value passed back on the iTimer_Proc to distinguish between them. Then you could have a private enum for the different timers, and keep most up the effeciency that this code sets the standard for. Currently the workaround that I have is a tiny class that has it's own timer, and raises an event to be handled in the usercontrol. Any ideas?
(If this comment was disrespectful, please report it.)

 
2/9/2004 5:58:20 PMPaul Caton

Luke, there's a number of ways that this could be tackled, yours is good, either with or without a more efficient implemented interface (I would do that if the control was compiled to an ocx). Alternatively, seeing as a UserControl has an hWnd you could use the Subclasser with the SetTimer/KillTimer api... Or thirdly, let me take a look (in a couple of days) and i'll see if I can build it in without adding complication for those that don't care.
(If this comment was disrespectful, please report it.)

 
2/13/2004 7:39:30 AMPaul Caton

As per Luke's enquiry I've added support to identify the source timer in the callback -- this is a legitimate request because unlike the subclasser where you have the hWnd to clue you as to source... should you have multiple timers -- you'd have no idea as to which had fired. tlb updated to 1.03 -- Improved some misleading asm comments.

(If this comment was disrespectful, please report it.)

 
2/13/2004 9:54:02 AM

Great job!!!
Thank you for this!
(If this comment was disrespectful, please report it.)

 
2/14/2004 1:49:03 AMMArio Flores G

MVP MR.Caton..very well done..I give you ********* 5 excelent globes..Would it be possible or are any hooks that monitor(spy) hardware..like hdd activity?...
(If this comment was disrespectful, please report it.)

 
2/14/2004 5:56:41 AMPaul Caton

Mario, thanks... Alex Ionescu would be the man for that. My *guess* is that you'd need a driver in between the OS and the hardware driver. Sysinternals did one such a few years ago (filemon)... but they never published the source. It may be possible to patch the internal syscall tables to achieve the desired result but as you can imagine.. this is extremely hardcore.
(If this comment was disrespectful, please report it.)

 
2/15/2004 12:57:01 AMEmiliano Scavuzzo

Great code Paul but I need your help to solve a problem. I'm working on CSocketMaster, best winsock control substitute ever, but I'm having problem to subclass with your code. If I could solve this problem no one would ever use winsock control again. If you are interested I'll send you more info.
Emiliano Scavuzzo
(If this comment was disrespectful, please report it.)

 
2/15/2004 5:50:05 AMPaul Caton

Emiliano... sure, send it along. I'm pretty familiar with sockets, have my own socket class so I'm sure I can fix it for you.
(If this comment was disrespectful, please report it.)

 
2/16/2004 1:24:04 PM

Excellent code! 5 stars!!!

Should there be a warning about not using this in Win9x? Everything looks great in XP, but when I run the samples in Win98 (at least the ones that will run) they either don't work or crash. In fact if I run the ApiWindow example in the IDE and choose 'YES' to the msgbox, I have to reboot just to get back into VB.

Please advise if there is a minimum system requirement or if I am just the "lucky one" with the problems.

Thanks for this hardcore submission.
(If this comment was disrespectful, please report it.)

 
2/16/2004 1:33:33 PMPaul Caton

I think you're the lucky one,

the samples should work with Win98 - Except, I'm guessing, the hooks. Unfortunately I don't have any access to Win98 - but I certainly haven't heard from anyone else in this regard. Anyway, I'll see if I can get someone to test them for me.
(If this comment was disrespectful, please report it.)

 
2/18/2004 12:23:37 AMMArio Flores G

Mr Caton is there anyway to force the keyboard to read while windows logon screen is on in XP?..Lets say Im running WINXP and type "Windows key + L" this leads me to the logon Screen
and i whant to read the password text under the "user" field (yust for fun :p)
doesn't hook or reads anything.
http://www.codeguru.com/activex/ComHook.html cheers Paul !! ;)

(If this comment was disrespectful, please report it.)

 
2/18/2004 4:45:21 AMPaul Caton

Mario, as far as I know the Windows login takes total control of the keyboard. It might be possible with a driver. Naughty!
(If this comment was disrespectful, please report it.)

 
2/21/2004 1:42:12 AMMasino Sinaga

Paul, This is the best code I've ever found in PSC and this is just like I need. Your code even can detect and save the value of the keyboard if user is using MS Excel app (many code example in PSC cannot detect the value of the keyboard if user is using Excel app.). Thanks for sharing this. Of course, 5 globes from me.
(If this comment was disrespectful, please report it.)

 
2/21/2004 4:05:44 AMPaul Caton

Thanks Masino.. well, the best code is always the code you are looking for, especially if you didn't expect to find it. The Office applications seem to take control of the keyboard at a lower level than is normal for a Windows app. AFAIK only the Windows login is safe from the global keyboard hook.
(If this comment was disrespectful, please report it.)

 
2/21/2004 3:09:43 PMMArio Flores G

How about Fade effect When user Closes window( Maximized style ) ;)
(If this comment was disrespectful, please report it.)

 
2/25/2004 5:28:00 PM

Hmmmm... Verrry very interesting. Full marks from me Paul. Innovatite ideas & attempts, neat & safe code, quick responses to feedback. All these are the hallmarks of a "legend". ;-) Keep up the good work ! And I am sure you must be delighted with the positive feedback from the other "legends" at PSC.
(If this comment was disrespectful, please report it.)

 
2/25/2004 6:22:40 PM

Paul, this package includes some of the cleverest code I've ever seen anywhere! I really enjoyed showing your cShift class to other VB coders and asking them if they could explain how it works. Excellent job! Is there any chance you'll be enhancing the subclasser to handle multiple objects or do you recommend we write our own wrapper class?
(If this comment was disrespectful, please report it.)

 
2/26/2004 8:22:41 AMPaul Caton

Dean,
thanks for the kind comments. Understand that I didn't invent class
patching. Multiple objects: You mean you want to Subclass, for example, a
form and a picture box on the same form.. is that what you mean? That being
so, you just create two instances of cSubclass - they will of course
callback to the same callback routine, but you can distinguish the source
based on the hWnd.
(If this comment was disrespectful, please report it.)

 
2/26/2004 2:08:55 PMEmiliano Scavuzzo

Hi%2C+I+was+wondering+if+it+is+necessary+to+check+if+IDE+is+on+a+breakpoint+when+you+cal l+EbMode+on+the+subclassing+WndProc%2E+I+tested+it+without+the+check+and+worked+fine%2E+The+messages +were+send+back+to+my+code+even+when+a+BP+was+set%2E+I+need+to+know+if+this+is+safe+cause+I+don%27t+ want+to+lose+messages+while+IDE+is+on+a+BP%2E
(If this comment was disrespectful, please report it.)

 
2/26/2004 2:14:59 PMEmiliano Scavuzzo

What the heck was that?
Ok here it goes again:
I was wondering if it is necessary to check if IDE is on a breackpoint when you call EbMode on the subclassing WndProc. I tested it without the check and worked fine. The messages were sent back to my code even when a BP was set. I need to know if this is safe cause I don't want to lose messages while IDE is on a BP.
(If this comment was disrespectful, please report it.)

 
3/17/2004 1:25:54 AMM.Naveen Swamy

nice work buddy 5 stars
(If this comment was disrespectful, please report it.)

 
6/2/2004 2:08:34 AM

Please Accept my admiration for the amazing code. Its beautiful what you made VB do. But I had many problems when trying to use the subclasser in my application. I was trying to subclass an external window, say msn, and the subclass assert kept failing when I called sc.subclass(hTgt, me) where htgt is the hwnd of the external window that was returned by FindWindow Api. I failed to undersatnd how to make this work after 4 hours of head scratching. An explanation would be greatly appreciated. All the examples seem to only use Me.hwnd is that all it was designed to do?
(If this comment was disrespectful, please report it.)

 
8/11/2004 3:38:59 PM

Hi Paul,
That's an awesome piece of code. 5 big starts from me too!! Ofcourse, I'm taking a long time just to follow it even with the ample comments in considering I'm a newbie :) I had a question though: I'm trying to implement something like the task manager in Windows XP in VB which means I need to know when a new application is launched or an existing one has terminated. So I assume I need to use a global hook - specifically the WH_SHELL hook and look for the HSHELL_WINDOWCREATED and HSHELL_WINDOWDESTROYED codes in the ShellProc callback procedure. Does this mean I *have* to implement this in C++/VC++ and there's no way I can implement the hook in VB? If there's a better way to accomplish what I'm attempting to do, I would be happy to know from a pro :) Thanks!
(If this comment was disrespectful, please report it.)

 
10/15/2004 5:58:10 AMRichard Mewett

Top of the class again! Rare sample of high quality code & high quality GUI
(If this comment was disrespectful, please report it.)

 
11/8/2004 6:57:57 AM

The code for the keyboard hooks is not quite correct when dealing with Ctrl key. It is returning eg. ctrl+a will be returned as ctrl+A. It would be useful to also know whether the Shift key has been used eg. for hotkey usage of the likes of Ctrl+Shift+a or Alt+shift+a. Thanks for your code, very useful.
(If this comment was disrespectful, please report it.)

 
12/3/2004 5:27:11 PM

Nice code. Do you have any examples of using both mouse and kb hooks together? Also, how do you detect mouse button events?
(If this comment was disrespectful, please report it.)

 
4/23/2005 9:52:01 AMTom Pydeski

Paul,
You probably know this, but if you wanted to hook multimedia keyboard keypresses, you can use the following info.
Hookstruct.Flags bit 0 is for extended key (Multimedia keyboard)
in some cases the scan code corresponds to the appcommand key
in others, the vkcode=255 and scan code is something else
bit 8 is the keydown/keyup flag (128) 1 =key up
my hp's one-touch keys all return the same vkey and scan code, but another multimedia keyboard works fine.

(If this comment was disrespectful, please report it.)

 
4/25/2005 4:59:28 AM

Five starts!!
(If this comment was disrespectful, please report it.)

 
10/28/2005 6:23:09 AMkanguru

5 stars, this really the greatest code for hooking keystrokes in VB6 i 've ever found.
(If this comment was disrespectful, please report it.)

 
12/9/2005 10:18:21 PMThe_Jokez

500 Stars !
Absolutely fabulous !
Perfect.
(If this comment was disrespectful, please report it.)

 
3/29/2006 10:23:09 AMBob Hamilton

Congrats on the award - this code deserves it. However I have had problems using it in the IDE on a slow machine (PIII-550) with lots of Debug.Prints in the hook handler. Is this code re-entrant?
(If this comment was disrespectful, please report it.)

 
2/18/2007 5:21:10 AMYogi Yang

great work. I really like it.

Unfortunately I can will not be able to use it in my apps as most of my cutomes still use Win9x.
(If this comment was disrespectful, please report it.)

 
12/26/2007 6:24:12 PMDr.ALi-Satan

Nice JOB! Could U Explain how to use u r C++ Dll In VB6:D I'm Confused.And could u explain how to use injected Dll For Reveal password from password box:D Anyway 5 * from me. Tnx For ur Help!
(If this comment was disrespectful, please report it.)

 
5/12/2011 2:54:21 PMyash

Nice submission ... very good for learning hooks
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.