Archived

This topic is now archived and is closed to further replies.

what is (HWND) really?

This topic is 4951 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what is (HWND) really? #ifdef STRICT typedef void *HANDLE; #define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name #else typedef PVOID HANDLE; #define DECLARE_HANDLE(name) typedef HANDLE name #endif typedef HANDLE *PHANDLE; DECLARE_HANDLE (HWND); I have been using HWND hWnd; for 4 years now and I really don''t konw what it is? what''s this -> name##__ *name; struct name##__ { int unused; }; What the hell. (int unused), is not used then why have (int unused) in there? Is HWND an int,pointer,const char* ? A pointer to a (struct) with one (var) thats not used? You have to have HWND why? if windows does not use (int unused) what does it use then? the pointer to nothing?

Share this post


Link to post
Share on other sites
It''s a 32 bit sized variable that you can''t do anything with except pass to windows functions. Treat it as an opaque type; don''t try to understand what goes on inside it. The OS knows what it is, and that''s good enough. I love Big Brother.

Share this post


Link to post
Share on other sites
It''s a handle that manifests itself as either a pointer to a struct or a pointer to void. It doesn''t so much matter, as Windows handles it consistently internally. While the struct member is unused if present (as its name indicates), what IS used is the address to which the pointer points. What this means is that the HWND, as a pointer, doesn''t really point to anything; it...

Oh man, I just got dumped for prom. If I''m feeling like it, maybe I''ll add on to this later. Hope you can glean some useful info from the above.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
Windows doesn''t even necessarily use it as a pointer. Some handles are interpreted as integer values and used as an index into tables held by the operatng system. Either way it doesn''t matter. Just treat it as a 32 bit black box and don''t think about it any further.

Share this post


Link to post
Share on other sites
Same has above - it''s basicaly a numeric value used to identify some object. I''ve heard that windows used to just use the pointer to that object in memory (since it''a a simple way of getting a unique identifyer for that object), but people kept on using it to make changes to the object without going through the apis so Microsoft stopped (or is trying to stop) doing that.

Share this post


Link to post
Share on other sites
quote:
what''s this -> name##__ *name;

struct name##__ { int unused; };
What the hell.


## is an interesting thing you can use, but only in macros. Basically, it combines the left hand side with the right hand side to form one string. For example,

#define ADD_ABC_TO_END(param) param ## ABC

you can use the macro like this
ADD_ABC_TO_END(hello)
and the compiler will turn this line into
helloABC

Not very useful most of the time, but DECLARE_HANDLE uses it to declare custom structs quickly, as shown here. When it says DECLARE_HANDLE(HWND), the compiler turns this line into struct HWND__{ int unused; };

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites