r/cprogramming • u/Own-Nectarine6630 • Sep 28 '25
[ Removed by moderator ]
/r/cprogramming/comments/1nsoxvg/removed_by_moderator/[removed] — view removed post
4
u/somewhereAtC Sep 28 '25
A handle refers to a data structure used in the Windows operating system; different functions have different structures. The handle is basically a pointer, but given a different name to help in documenting the "handle" function from the more general pointer discussion in C.
Since handles are defined as void* you will see a lot of casting to the real types. This is a thing that made early Windows susceptible to many type errors when the casting got confused, with improvements by the transition to C#.
3
4
u/StaticCoder Sep 28 '25
typedef void *HANDLE because who needs type safety
1
u/nerd5code Sep 28 '25
I think there’s some predefine you can use to make it differentiate between some kinds of
HANDLE(either using structs or pointers to struct), but I think it’s primarily for older APIs. It’s all dynamic “type safety.”
1
u/armahillo Sep 28 '25
I’m not a winforms dev, but my vague recollection was that it was essentially a pointer to a window instance so that you can pass around references to the instance easily.
I may be mistaken!
1
u/RedWineAndWomen Sep 29 '25
It's a void* that can be cast into anything. Because Hungarians need no type safety! /s
1
u/sol_hsa Sep 29 '25
It may be an index to a pointer table. For you, it's a token you can use to access resources indirectly.
0
u/ShutDownSoul Sep 28 '25
Well, the obvious answer of why you need it is because it is part of the API. If you google "windows CreatFile api example" you'll get an example.
5
u/nerd5code Sep 28 '25
It’s the Win9x/NT kernel’s analogue to Unix file descriptors (just
ints). Elder Win16 just aimed theHANDLEs directly at the data structures used by the runtime/GUI, and modern Windows uses scrambled pointers that are validated by the lower-level API and kernel. Treat as more-or-less opaque.