The essence of the handle - chaos anyway

zhaozj2021-02-08  212

Affected by M $ help documentation and a lot of Windows programming books, everyone is more common to the duty handle is: the handle is an integer to identify the Windows object, the handle is not a pointer ...

In fact, these are just the scorpion of the data package. Let's take a look at what is Handle.

Please find Windef.h to handle the vast majority of the definition: DECLARE_HANDLE (HWND); DECLARE_HANDLE (HHOOK); ...... DECLARE_HANDLE (HGDIOBJ); DECLARE_HANDLE (HBITMAP); DECLARE_HANDLE (HBRUSH); ...... typedef HANDLE HGLOBAL; typedef HANDLE HLOCAL ...... ok, now you turn it with me to Winnt.h, see what declare_handle and handle is: #ifdef stricttypedef void * handle; #define declare_handle (name) struct name ## __ {int unused;}; typedef struct name ## __ * name # elsetypedef PVOID HANDLE; #define DECLARE_HANDLE (name) typedef HANDLE name # endiftypedef HANDLE * pHANDLE; ha ha, now know it, HANDLE is PVOID, which is untyped pointer, and DECLARE_HANDLE (HWND); Is: struct hwnd__ {int unused;}; typedef struct hwnd__ * hWnd; now it is actually clear, these handsles are all pointing to the struct's pointer, as for this Struct's use, even M $ said unused, ^ o ^ Now explain the meaning of M $, this is the so-called data package, you can pass the M $'s internal structure pointer in your program, but you don't know what it points to the content, and you can The wrong words of the handle prevent everyone from questioning :). And the M $ program can be written: #include // This is the same as you use #include "windows_in.h" // This is M $ self-use, you don't want to see ^ o ^ Hsomethingelse DOSMETHING (HSomething hsomething) {struct realsomething * p = (struct realsomething *) hsomething; // First mandatory type to convert into internal structural pointers ... do something ... reburn (hsomethingelse) prealogsomethingelse; // Mandatory type reverse convert} ^ o ^ ^ o ^ ^ o ^ ^ o ^ ^ o ^ ^ ^ ^ o ^ ^ o ^ ^ ^ ^ o ^ ^ ^ ^ o ^ ^ o ^ ^ ^ ^ o ^ ^ o ^

转载请注明原文地址:https://www.9cbs.com/read-2586.html

New Post(0)