What is a window handle?

Simply what is a window handle?
And what are the properties it can hold?

I've been through a fair few books that talk about windows handles (even windows tutorials) without any mention to the above points.
Simply what is a window handle?


A "handle" is a generic identifier (typically a pointer) used to represent something.

The handle itself you never use directly, you just pass it around to functions that use it.

In Windows, handles are represented as an HWND. The actual value of the HWND is meaningless to you... and you don't need to be concerned with it. Just know that a given HWND represents a window.

You will pass HWNDs to various functions, which lets those functions know which window they are to be working with.

And what are the properties it can hold?


As far as you care: none.

It's just an id.
Amplifying what Disch said, sometimes you'll see the term 'Opaque Pointer'. Meaning something to the effect that you can't really clearly see what its pointing at. Only Windows knows that.

I believe somewhere deep within the Windows headers its ultimately typedefed of defined as a void*.

One thing about it, as Disch said, is that its oftentimes passed to various Api functions that work with a window in some way, and I don't believe I've ever seen it as anywhere but in the first position as a function parameter. In many ways its very much like C++'s 'this' pointer. That analogy actually works very well. The HWND parameter identifies the actual instance of some Window Class to which some operation is to be performed, as is exactly the case with the implicit 'this' pointer. And while mulling over that, consider too that when doing OOP in C as opposed to C++, one calls methods in exactly that fashion, i.e., one passes the 'this' pointer to the method explicitely as the 1st parameter. Then add to that, that Microsoft's Windows interface is indeed C based, that is, C based OOP. So there you have possibly the true significanse of HWNDs.
Thanks guys!
So I suppose when I see a handle in my books I'll have to do some digging to try and find the class that it refers to in each particular instance.
typedef void* HWND;

I believe somewhere deep within the Windows headers its ultimately typedefed of defined as a void*.

@freddie1
It is :).
Last edited on
Do you know exactly where that's at in the headers DeXecipher? Seems I've looked but never found it. Which one is it in? I think my knowledge of it being ultimately defined as a void* came from other posters in this forum.
I've seen it somewhere too - it is a macro wrapped in a macro - I'll look it up as well if I can find it (intellisense should find it no problem) - I also believe it might also have some windows version dependencies.


EDIT
Using intellisense from MSVC2012 (with SDK 8.0 installed) - sends me to this line in Windef.h header
1
2
3
4
5
6
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)

DECLARE_HANDLE            (HWND);
DECLARE_HANDLE            (HHOOK);

#endif /* WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) */ 


DECLARE_HANDLE is defined (in winnt.h header) as
1
2
3
4
5
6
7
8
9
10
11
#ifdef STRICT
typedef void *HANDLE;
#if 0 && (_MSC_VER > 1000)
#define DECLARE_HANDLE(name) struct name##__; typedef struct name##__ *name
#else
#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
#endif
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif 



Last edited on
Thanks guestgulkan. Its certainly a bit obscure, to say the least.
Topic archived. No new replies allowed.