Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Subclassing and API Hooking?

I am fairly new to Windows API Programming and I want to know in layman's terms what is subclassing and API Hooking. I am doing a project that uses owner drawn controls, and I two terms keep coming up. Now I have already seen a lot of detailed tutorials on CodeProject.com about these topics, but the problem is that all of them use MFC, and I am coding in pure Win32. If anyone knows about any good tutorials of the above mentioned topics then please post the links. Also try to avoid links to the msdn, as novice I am having trouble making sense of whats written there.

like image 354
Sreyan Avatar asked Dec 17 '22 00:12

Sreyan


2 Answers

Layman terms: sub-classing is done by replacing the window procedure of a window. Which redirects the calls that Windows makes to deliver a message to a window to your function so you get to see those messages first. This lets you change the behavior of the window, giving it new capabilities that the original one didn't have. It is a very common technique and directly supported by the OS, the SDK article is here.

API hooking is similar but for winapi functions. So that you can change the behavior of code that runs in your process that you didn't write. Arbitrarily, you could hook CreateFile() and change the passed file name or make it fail on purpose. It is much less common to do this and also much harder to get right since it is not a baked-in capability like sub-classing is. Microsoft's Detours is an example implementation of the technique.

Either technique is in the advanced programming category and you can get yourself into pretty nasty trouble if you don't do it right. If you have trouble reading the MSDN articles right now then leave it on the shelf until you're ready for it.

like image 74
Hans Passant Avatar answered Dec 18 '22 13:12

Hans Passant


If you are programming raw Win32 you should probably get the Petzold book "Programming Windows".

like image 43
Ben Avatar answered Dec 18 '22 13:12

Ben