views:

84

answers:

2

This is Line 519 of WinNT.h (BUILD Version: 0091)

#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name

Why do we need a pointer to an struct with a single int member with a weird name called unused?

And will we ever need to use a line of code like this one?

HINSTANCE hInstance = new HINSTANCE__;

Overall declaring different data types with the same structures, doesn't make sense to me. What's the idea behind this?

DECLARE_HANDLE(HRGN);
DECLARE_HANDLE(HRSRC);
DECLARE_HANDLE(HSPRITE);
DECLARE_HANDLE(HLSURF);
DECLARE_HANDLE(HSTR);
DECLARE_HANDLE(HTASK);
DECLARE_HANDLE(HWINSTA);
DECLARE_HANDLE(HKL);
+1  A: 

And will we ever need to use a line of code like this one?
HINSTANCE hInstance = new HINSTANCE__;

You usually use a HINSTANCE value returned by a Windows system call; I have never seen code executing a line like that.

kiamlaluno
+3  A: 

The point is for the different handles to have different types so that, for example, a HINSTANCE isn't assignable to a HANDLE. If they were all defined as "void*", then there are classes of errors that the compiler could not detect.

janm
So, the idea behind it is to have integer data types which will not be used for purposes other than what they were declared for?
David Weng
Yes, that provides type safety for handle types so you get an error when you try to pass a HINSTANCE to a function that requires a HANDLE even though the underlying structure is identical. (This answer was going to be just "yes", but that evidently wasn't verbose enough. Extra verbosity added.)
janm