开发者

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

开发者 https://www.devze.com 2023-01-10 19:38 出处:网络
This is Line 519 of WinNT.h (BUILD Version: 0091) 开发者_运维知识库#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name

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);


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消