-
Tricky #define macro...
When looking up HINSTANCE (yes, I know what it means but I wanted to see the var tp) i see that its defined by DECLARE_HANDLE, a rather advanced macro:
Code:
#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
Could someone take the time to explain what it means? I figured a handle would be just number (prob. Unisgned Int 32) but this is something else... ant it's not a struct with any members either..
-
Code:
DECLARE_HANDLE(HINSTANCE); will expand to:
struct HINSTANCE__ { int unused; }; typedef struct HINSTANCE__ *HINSTANCE;
Windows declares handles as pointers to dummy structures for type safety. This means the compiler can give a warning if you try to assign an HINSTANCE to another type. If it was simply declared as a void*, there would be less type safety.
You can consider a handle as a numeric value rather than a pointer to anything. For most handle types, the meaning of the number is opaque. However, both a HINSTANCE and a HMODULE are documented as containing the base address of the module they refer to.
-
So of the two statements...
Code:
struct HINSTANCE__ { int unused; };
typedef struct HINSTANCE__ *HINSTANCE;
...the first declares the dummy type "HINSTANCE__" and the second declares "HINSTANCE" (without underscores) as a pointer to a variable of that type?
In VC6 this is also okay to the preproc:
Code:
struct HINSTANCE__ { int unused; };
typedef HINSTANCE__* HINSTANCE;
I hope I figured it out right. The first looks much trickier, because there is the word "struct" after typedef (we've already made the struct), and no space between the original type (HINSTANCE__*) and it's alias HINSTANCE. Likewise it's also clearer with int* ip than int *ip in my opinion because it follows type variable format as in int i
-
> struct name##__
The ## is the pre-processor token pasting operator. The left token is appended with the right token.
> Likewise it's also clearer with int* ip than int *ip in my opinion
Until you see this
int* p, q;
What type is q ?
-
>> ...the first declares the dummy type "HINSTANCE__" and the second declares "HINSTANCE" (without underscores) as a pointer to a variable of that type? <<
Yep.
>> In VC6 this is also okay to the preproc: <<
Only in C++. In C, the struct keyword is required.