DWORD and DWORD_PTR on a 64-bit machine

Several types have been added to the Windows API *_PTRto support 64-bit Win64 addressing.

SetItemData(int nIndex,DWORD_PTR dwItemData)

This API works for both 64 and 32 bit machines when I pass the second parameter as DWORD.

I want to know if this particular API will fail on a 64-bit machine if I pass the second parameter as DWORD. How to check crash script?

Thank you Nihil

+3
source share
1 answer

The function does not crash if you pass DWORDbecause it fits in DWORD_PTR. However, the pointer must be placed on DWORD_PTR, but not DWORDon 64-bit platforms.

So this code is correct:

int *before_ptr = new int;
yourListBox.SetItemData(index, (DWORD_PTR) before_ptr);
int *after_ptr = (int *) yourListBox.GetItemData(index);
ASSERT(before_ptr == after_ptr);  // Succeeds.
delete after_ptr;                 // Works.

32 :

int *before_ptr = new int;
yourListBox.SetItemData(index, (DWORD) before_ptr);
int *after_ptr = (int *) yourListBox.GetItemData(index);
ASSERT(before_ptr == after_ptr);  // Fails.
delete after_ptr;                 // Undefined behavior, might corrupt the heap.
+3

All Articles