BUG: DAO 3.6 Causes Debug Errors in MFC DAO Non-Unicode BuildsID: Q235507
|
When converting an MFC DAO application to use DAO 3.6 as described in the Knowledge Base article Q216152 PRB: Unrecognized Database Format Error with Access 2000 Database, the application can produce a debug error. This is the exact error message:
DAMAGE: after Normal block (#204) at <hex address>
DAO 3.6 incorrectly overwrites the character buffer supplied to it by MFC when it fetches text data. It first copies the Unicode string into the buffer that is twice as long as the length of the buffer and then converts the string to an Ansi string.
MFC defines a function called DaoStringAllocCallback in Daodfx.cpp. This function reallocates the internal CString buffer to the size necessary to hold the data and passes the buffer back to DAO. DAO then fills the buffer with the data. It is possible to define a different callback function that allocates twice the necessary bytes and changes the cbDataOffset member of the DAO binding structure to point to this function. See the More Information section below for details.
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article.
AfxGetModuleState()->m_dwVersion = 0x0601;
STDAPI MyDaoStringAllocCallback(DWORD dwLen, DWORD pData, void** ppv);
STDAPI MyDaoStringAllocCallback(DWORD dwLen, DWORD pData, void** ppv)
{
LPTSTR lpsz;
CString* pstr = (CString*)pData;
dwLen++;
TRY
{
//Allocate twice the space needed so that DAO does not overwrite the buffer
lpsz = pstr->GetBufferSetLength(2*dwLen/sizeof(TCHAR));
*ppv = (void*)(dwLen > 0 ? lpsz : NULL);
}
CATCH_ALL(e)
{
e->Delete();
return E_OUTOFMEMORY;
}
END_CATCH_ALL
return S_OK;
}
if (pFX->m_nOperation == CDaoFieldExchange::BindField)
{
for (int i=0;i<this->m_nFields;i++)
{
LPDAOCOLUMNBINDING pcb = &m_prgDaoColBindInfo[i];
if (pcb->dwDataType == DAO_CHAR)
{
pcb->cbDataOffset = (DWORD)MyDaoStringAllocCallback;
}
}
}
Q216152 PRB: Unrecognized Database Format Error with Access 2000
Q236056 PRB: DAO 3.6 Overruns buffers in GetRowsEx method
Additional query words: DAO AppWizard MFC MoveNext
Keywords : kbDatabase kbMFC kbVC600 kbGrpVCDB kbDAO360 kbDAO360bug
Version : winnt:6.0
Platform : winnt
Issue type : kbbug
Last Reviewed: July 13, 1999