I don't know exactly what happens. But I have some notes:
The first two functions are for dropping.
Your code will fail when your project is an Unicode application.
Depending on your Unicode project setting, you must use the matching
CF_TEXT
or
CF_UNICODETEXT
format. Note also that there is no implicit conversion between Unicode and ANSI when using Drag & Drop. So the final application should cache strings in both formats.
Your code does not show the definition of the
DropData
object. It must not be a member of your view class. Define it locally.
You may try this to check if it solves your problem:
void CFilesDragDropView::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult)
{
LPCTSTR s = _T("ola mundo");
size_t nSize = (_tcslen(s) + 1) * sizeof(TCHAR);
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, nSize);
memcpy((char*)::GlobalLock(hMem), s, nSize);
::GlobalUnlock(hMem);
COleDataSource * pDataSrc = new COleDataSource;
#ifdef _UNICODE
pDataSrc->CacheGlobalData(CF_UNICODETEXT, hMem);
#else
pDataSrc->CacheGlobalData(CF_TEXT, hMem);
#endif
DROPEFFECT de = pDataSrc->DoDragDrop(DROPEFFECT_COPY);
pDataSrc->InternalRelease();
*pResult = 0;
}
The above example allocates the
COleDataSource
object on the heap. This is the preferred way. When doing so,
InternalRelease()
must be called afterwards when not calling
SetClipboard()
.