Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this line of C/C++ preprocessor mean?

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);
like image 350
David Weng Avatar asked Aug 07 '10 07:08

David Weng


3 Answers

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.

like image 69
janm Avatar answered Nov 28 '22 11:11

janm


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.

like image 24
apaderno Avatar answered Nov 28 '22 09:11

apaderno


They don't actually point to anything to memory; they are just used to refer to objects (files, resource, semaphores, windows) when making calls to the Windows API. While they're nothing more than just indexes into kernel's object tables, the developers decided that they make it a pointer to an unused structure which would make them "opaque" and cause less confusion between other types. The DECLARE_HANDLE is a function macro that does just that - declaring opaque types for handles.

like image 28
minmaxavg Avatar answered Nov 28 '22 09:11

minmaxavg