I am writing two lines in memory using CMemFile::Write()
:
void CLISTCTRLDlg::Export(LPTSTR *pBlock)
{
CMemFile outMem(32768);
CString csHeader = _T("EmpId EmpName EmpAddress\n");
outMem.Write(csHeader.GetBuffer(0), csHeader.GetLength());
CString csInfo = _T("1 TestName TestAddress\n");
outMem.Write(csInfo.GetBuffer(0), csInfo.GetLength());
long lLen = outMem.GetLength() + 1;
BYTE *mBlock = outMem.Detach();
*pBlock = (LPTSTR) malloc(sizeof(char) * lLen);
memcpy(*pBlock, mBlock, lLen-1);
(*pBlock)[lLen -1] = 0;
OutputDebugStringW(*pBlock);
free(outMem);
}
The output window shows the string "EmpId EmpNam? ???????? ?????????" when the OutputDebugStringW(*pBlock); statement is executed.
I do not understand why the data is truncated.
Further more, the system throws an unhandled exception when the statement free(outMem); is executed.
Can anyone please guide me to the solution and let me know where I am mistaken?
Thanks.