Why is the exception thrown on memcpy using during copying LPBYTE to LPTSTR (clipboard)?
- by user46503
Hello,
I have a LPBYTE array (taken from file) and I need to copy it into LPTSRT (actually into the clipboard). The trouble is copying work but unstable, sometime an exception was thrown (not always) and I don't understand why. The code is:
FILE *fConnect = _wfopen(connectFilePath, _T("rb"));
if (!fConnect)
return;
fseek(fConnect, 0, SEEK_END);
lSize = ftell(fConnect);
rewind(fConnect);
LPBYTE lpByte = (LPBYTE) malloc(lSize);
fread(lpByte, 1, lSize, fConnect);
lpByte[lSize] = 0;
fclose(fConnect);
//Copy into clipboard
BOOL openRes = OpenClipboard(NULL);
if (!openRes)
return;
DWORD err = GetLastError();
EmptyClipboard();
HGLOBAL hText;
hText = GlobalAlloc(GMEM_MOVEABLE, (lSize+ sizeof(TCHAR)));
LPTSTR sMem = (TCHAR*)GlobalLock(hText);
memcpy(sMem, lpByte, (lSize + sizeof(TCHAR)));
The last string is the place where the exception is thrown.
Thanks a lot