Pointers / drag and drop
-
Hey i've been trying to get drag and drop to work with my own data (had it working with text) i think i'm getting confused with pointers, here my lastest try, this is in a listview using WTL and the drag and drop classes from code project
DragData dragData;
.. get data from selected items and add it to dragData ..
..medium.hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(dragData));
DragData *pMem = (DragData *)GlobalLock(medium.hGlobal);
(*pMem) = dragData;
GlobalUnlock(medium.hGlobal);I'm not sure whats wrong but it always crashes i think its the (*pMem) = dragData; but i'm not sure why, i've tried many different way, sometimes i got it not to crash but then the data is never at the 'drop' end. thanks for any help, been annoying me for awhile now, cya Luke.
-
Hey i've been trying to get drag and drop to work with my own data (had it working with text) i think i'm getting confused with pointers, here my lastest try, this is in a listview using WTL and the drag and drop classes from code project
DragData dragData;
.. get data from selected items and add it to dragData ..
..medium.hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(dragData));
DragData *pMem = (DragData *)GlobalLock(medium.hGlobal);
(*pMem) = dragData;
GlobalUnlock(medium.hGlobal);I'm not sure whats wrong but it always crashes i think its the (*pMem) = dragData; but i'm not sure why, i've tried many different way, sometimes i got it not to crash but then the data is never at the 'drop' end. thanks for any help, been annoying me for awhile now, cya Luke.
Hi, I think you are right, when you think it`s the line (*pMem) = dragData. Try this one instead :
pMem = &dragData;
The difference is pMem is the address of some storage, which may be assigned. (*pMem) is the storage itself, which may not be assigned, unless its a simple type (int, char etc. ) or it's assignment operator is overloaded. The next thing is, pMem is the storage you get from the system, to put some values in. So my code will crash too. So use a memory copy function likememcpy(pMem,&dragData,sizeof(dragData)
. I think this will do the job. G. Steudtel -
Hey i've been trying to get drag and drop to work with my own data (had it working with text) i think i'm getting confused with pointers, here my lastest try, this is in a listview using WTL and the drag and drop classes from code project
DragData dragData;
.. get data from selected items and add it to dragData ..
..medium.hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(dragData));
DragData *pMem = (DragData *)GlobalLock(medium.hGlobal);
(*pMem) = dragData;
GlobalUnlock(medium.hGlobal);I'm not sure whats wrong but it always crashes i think its the (*pMem) = dragData; but i'm not sure why, i've tried many different way, sometimes i got it not to crash but then the data is never at the 'drop' end. thanks for any help, been annoying me for awhile now, cya Luke.
In addition to the other suggestion, you should get into the habit of checking return values, especially for dynamically allocated memory.
-
Hey i've been trying to get drag and drop to work with my own data (had it working with text) i think i'm getting confused with pointers, here my lastest try, this is in a listview using WTL and the drag and drop classes from code project
DragData dragData;
.. get data from selected items and add it to dragData ..
..medium.hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(dragData));
DragData *pMem = (DragData *)GlobalLock(medium.hGlobal);
(*pMem) = dragData;
GlobalUnlock(medium.hGlobal);I'm not sure whats wrong but it always crashes i think its the (*pMem) = dragData; but i'm not sure why, i've tried many different way, sometimes i got it not to crash but then the data is never at the 'drop' end. thanks for any help, been annoying me for awhile now, cya Luke.
I think you ment to
DragData dragData;
.. get data from selected items and add it to dragData ..
..medium.hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(dragData));
DragData *pMem = (DragData *)GlobalLock(medium.hGlobal);(*pMem) = dragData;
//try coping memory, instead of passing stack(!?!?!?) variable
::memcpy(pMem, &dragData, sizeof(DragData));GlobalUnlock(medium.hGlobal);
P.S. what is the reason for using GlobalAlloc?