what is (HWND) really?

Started by
11 comments, last by markdezonia 19 years, 10 months ago
Google lead me here, I was trying to understand what DECLARE_HANDLE actually was too.

Looks to me like it''s mostly used to enforce type checking.

But the technical answer appears to be: "it''s a thang".

Sigh...I''ve been coding so long I guess...back in the day when we _did_ bother to understand such...thangs.

Cya

MikeInDallas
Advertisement
When expanded, the DECLARE_HANDLE(HWND) line looks like:

struct HWND__{    int unused;}; typedef HWND__ *HWND 


The ## preprocesser token is like the function strcat, except instead of concatenating strings at runtime, in concatenates strings at compile-time. When used cleverly with macros, you can easily create new variable or struct names.
A handle can bee seen as a user-mode identifier of a kernel-mode object.

For instance, if you call CreateFile (which is a function exported from kernel32.dll), that function is a thin wrapper that will cause a kernel-mode transition. The kernel will (via some drivers) open the file requested and keep some data related to that file (where it is on disk etc.). The "return value" from the kernel is some value, so that ReadFile/CloseHandle etc. can operate on that file.

What the handle actually is in the kernel isn''t really something you need to know about and in theory it could change. It could as mentioned be some index to a table, or a memory pointer used by the kernel.

The kernel obviously needs to do thorough checking to validate the pointer when you call CloseHandle etc. so you don''t pass a malicious value, or call CloseHandle twice with the same value.1

You can read about the "Object Manager" in Windows here: http://www.winntmag.com/

The object manager does not handle graphics, but you should get an understanding about how user-mode and kernel-mode relate to each other.

This topic is closed to new replies.

Advertisement