I have been reading up on IntPtr and have read that it is used to represent a Handle(s). What does this mean exactly? I'm sure it is a simple explanation, but the light bulb is just not turning on at the moment..
-
Duplicate question: http://stackoverflow.com/questions/1148177/just-what-is-an-intptr-exactly – HABJAN Apr 14 '11 at 18:03
-
@Habjan: Not quite. The OP is asking about handles, whereas that question is asking about `IntPtr`. – user541686 Apr 14 '11 at 18:04
-
possible duplicate of [struct which a HANDLE points to.](http://stackoverflow.com/questions/5343660/struct-which-a-handle-points-to) – Gabe Apr 14 '11 at 18:06
2 Answers
This is typically referring to an operating system Handle, and used internally. For example, Windows Forms uses an IntPtr
to refer to the Control's native Window Handle (HWND).
Handles in the Windows API are used for many things - most operating system related resources (files, sockets, windows, etc) are all exposed through a handle, which is effectively a pointer. In managed code, this gets stored in an IntPtr
.
That being said, IntPtr
is also regularly used to store pointers in interop scenarios, as well, as it automatically resizes based on 32bit or 64bit code.

- 554,122
- 78
- 1,158
- 1,373
-
+! for the use of HWND which is most often used in Windows API methods. This honestly is a really good answer. – Security Hound Apr 14 '11 at 18:19
A "handle" is an "opaque pointer". It's a value (usually just a number that's an index into an array) that the operating system gives to an application to represent an internal object, instead of giving it a pointer to the actual object. This is both for safety and abstraction reasons -- it forces the application to use the handle only through the provided APIs.

- 205,094
- 128
- 528
- 886