DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR When, how and why?

I found that Windows has several new Windows Data Types

DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR 

can you tell me when, how and why to use them?

+16
windows winapi mfc
Aug 13 '09 at 12:46
source share
2 answers

*_PTR were added to the Windows API to support Win64 64-bit addressing.

Since 32-bit APIs usually pass pointers using data types of the DWORD type, it was necessary to create new types for 64-bit compatibility, which could replace DWORD in 32-bit applications, but were expanded to 64 bits when used in 64-bit applications .

So, for example, application developers who want to write code that works like 32-bit or 64-bit Windows 32bit SetWindowLong(HWND,int,LONG) have been changed to SetWindowLongPtr(HWND,int,LONG_PTR)

In a 32-bit assembly, SetWindowLongPtr is just a macro that allows SetWindowLong , and LONG_PTR also a macro that allows LONG . On the other hand, with the 64-bit layout, SetWindowLongPtr is an API that takes 64 bits as the third parameter, and ULONG_PTR is a typedef for unsigned __int64 .

Using these _PTR types, one code base can be compiled for both Win32 and Win64 purposes.




When performing pointer arithmetic, these types must also be used in 32-bit code, which must be compatible with 64-bit.

so if you need to access an array with more than 4 billion elements, you will need to use INT_PTR, not INT

  CHAR* pHuge = new CHAR[0x200000000]; // allocate 8 billion bytes INT idx; INT_PTR idx2; pHuge[idx]; // can only access the 1st 4 billion elements. pHuge[idx2]; // can access all 64bits of potential array space. 
+26
Aug 13 '09 at 14:40
source share

Chris Becke is pretty much right. It’s just worth noting that these _PTR types are simply types that are 32-bit wide in a 32-bit application and 64-bit wide in a 64-bit application. It is so simple.

You can easily use __int3264 instead of INT_PTR, for example.

+5
Aug 13 '09 at 20:12
source share



All Articles