bufstring.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 1k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. HRESULT CBufferedString::SetBSTR(BSTR bstr)
  2. {
  3.     int len = lstrlenW(bstr) + 1;
  4.     if (len < ARRAYSIZE(m_szTmpBuf)) {
  5.         if (m_fFree) {
  6.             LocalFree(m_pBuf);
  7.             m_pBuf = m_szTmpBuf;
  8.             m_fFree = FALSE;
  9.         }
  10.     } else {
  11.         if (m_fFree) {
  12.             LPTSTR psz = (LPTSTR)LocalReAlloc(m_pBuf, len * SIZEOF(TCHAR), LMEM_MOVEABLE | LMEM_ZEROINIT);
  13.             if (psz) {
  14.                 m_pBuf = psz;
  15.             }
  16.             else {
  17.                 return E_OUTOFMEMORY;
  18.             }
  19.         } else {
  20.             LPTSTR psz = (LPTSTR)LocalAlloc(LPTR, len * SIZEOF(TCHAR));
  21.             if (psz) {
  22.                 m_pBuf = psz;
  23.                 m_fFree = TRUE;
  24.             }
  25.             else {
  26.                 return E_OUTOFMEMORY;
  27.             }
  28.         }
  29.     }
  30. #ifdef UNICODE
  31.     lstrcpy(m_pBuf, bstr);
  32. #else
  33.     WideCharToMultiByte(CP_ACP, 0, bstr, -1, m_pBuf, len, NULL, NULL);
  34. #endif
  35.     m_fSet = TRUE;
  36.     return S_OK;
  37. }