Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the size of HANDLE?

For an exercise, I look at the STARTUPINFO structure. As you can see the last 3 elements has the type HANDLE.

So I want to know which size HANDLE has. Do somebody know the size of HANDLE?

like image 431
user3097712 Avatar asked Sep 10 '14 16:09

user3097712


1 Answers

The Windows HANDLE type isn't a completely opaque type. Windows defines a couple of properties that you can depend on. The main one is the answer to your your question: it always of type void *. From the Windows Data Types entry on MSDN:

HANDLE

A handle to an object.

This type is declared in WinNT.h as follows:

typedef PVOID HANDLE;

Later on in the table you can see that PVOID is defined as void *.

So a HANDLE has the same size as void *. Or in other words, it's 32 bits when using a 32-bit compiler and 64 bits when using a 64-bit compiler. You shouldn't need to hard code either of these values in to your code, instead just use sizeof(HANDLE).

The other property of the Windows HANDLE type is very obscure, and is only barely documented: for kernel handles the bottom two bits are always zero. You shouldn't need to depend on this in your code, and hopefully you can see that you would never want to. I mention this for completeness and to emphasize how Microsoft has defined HANDLE to be more than just an internal implementation detail.

like image 115
Ross Ridge Avatar answered Sep 29 '22 11:09

Ross Ridge