Why ULONG Is 32-Bit Even on 64-Bit Windows

When you prepare C#'s DllImport functions, remember that if you see ULONG, you need to convert it as Int32 (more precise would be UInt32). Although one would assume that ULONG would be 64-bit on 64-bit systems, it is actually four bytes on both 32-bit and 64-bit systems.

Reason this happens is Microsoft's decision to use LLP64 data model for Windows API. This was done in order to ease switch to 64-bits for C++ programs. All API calls that were done on 32-bit systems that had ULONG in it, will work same even in 64-bit world. Easiest change is one that requires no action.

If you really want something with 64 bits, use ULONGLONG (or LONGLONG). This one is eight bytes in both 32-bit and 64-bit environment.

Beware if you see pointer to ULONG. Even if ULONG is four bytes on 64-bit platform, pointer to it is eight bytes. As matter of fact, all pointers are eight bytes.

One thought to “Why ULONG Is 32-Bit Even on 64-Bit Windows”

Leave a Reply

Your email address will not be published. Required fields are marked *