In my function to copy text I do the following:
// Allocate a global memory object for the text.
hglbCopy = GlobalAlloc(GMEM_MOVEABLE,
((text.length() + 1) * sizeof(WCHAR)));
if (hglbCopy == NULL)
{
CloseClipboard();
return;
}
// Lock the handle and copy the text to the buffer.
lptstrCopy = (LPWSTR)GlobalLock(hglbCopy);
memcpy(lptstrCopy, text.c_str(),
(text.length() + 1) * sizeof(WCHAR) );
lptstrCopy[(text.length() + 1) * sizeof(WCHAR)] = (WCHAR) 0; // null character CRASHES HERE
GlobalUnlock(hglbCopy);
When I copy a large chunk of text, it crashes when assigning the null terminator. Is there something wrong with my math in calculating how much memory to allocate?
Thanks
You don’t have to assign the null terminator yourself.
If you want to do it yourself do it like this: