6

There are few *_PTR types added to the Windows API in order to support Win64's 64bit addressing.

SetItemData(int nIndex,DWORD_PTR dwItemData)

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

I want to know, if this particular API will fail on 64 bit machine, if I pass the second parameter as DWORD. How can I test the fail scenario?

Thanks, Nikhil

Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
Nikhil
  • 299
  • 3
  • 6
  • 17

1 Answers1

6

The function will not fail if you pass a DWORD, because it fits into a DWORD_PTR. A pointer, however, is guaranteed to fit into a DWORD_PTR but not into a DWORD on 64-bit platforms.

Thus, 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.

But this code is wrong and will silently truncate the pointer to its lower 32 bits:

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.
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • You are correct the pointer will not fit into DWORD on 64 bit and it will truncate the pointer.How can i test this fail scenario where my pointer will truncate and i will get undesired value. – Nikhil May 03 '11 at 10:26
  • I tried this but i am not getting any assertion: 'CString str(""); for (int i=0;i < 20;i++) { str.Format(_T("item string %d"), i); XcomboCtr.AddString( str ); } int *before_ptr = new int; BOOL bsetItemData= XcomboCtr.SetItemData(0, (DWORD)before_ptr); int *after_ptr = (int*)( XcomboCtr.GetItemData(0)); ASSERT(before_ptr == after_ptr); // Fails. delete after_ptr; ' – Nikhil May 03 '11 at 11:41
  • @Nikhil, assuming you're building in debug mode (the `ASSERT` won't be compiled in release mode), it might mean that your pointer's upper 32 bits are all zeroes. In that case, it will work, but you can't guarantee that all your pointers will be like that. – Frédéric Hamidi May 03 '11 at 11:48