0

I using Visual C++ 2008 and ADO to access a database and obtain a field value like this:

_variant_t vtValue;

AfxVariantInit(&vtValue);

vtValue = m_pRecordset->Fields->GetItem(_variant_t(strFieldName))->GetValue();

If (vtValue.vt == VT_BSTR)
{
  strValue = vtValue.bstrVal;
  TRACE(_T(“Field value is %s.\r\n”), strValue);    // Cause CrtDbgReport: String too long or IO Error
}
else
{
   .. other codes…
}

The TRACE statement for strValue will cause the following error:

“CrtDbgReport: String too long or IO Error”

I just check strValue and found it is a Chinese string with only 6 characters, nothing special. Why it will cause the error?

Thanks

alancc
  • 487
  • 2
  • 24
  • 68

1 Answers1

1

A BSTR is a composite data type that consists of a length prefix, a data string, and a terminator, you can not assign it to CString directly as CString doesn't have the length prefix.

You can use the smart point class _bstr_t to do the conversion.

strValue = (TCHAR*)(_bstr_t)vtValue;
Matt
  • 6,010
  • 25
  • 36