Creating a blobclass for ATL

Environment: Configuration Files, INIs

-->

This class was written to make safearrays easy.

#ifndef _CFSAFEARVARIANT_
#define _CFSAFEARVARIANT_
/*
 *Author Frans Nagel
 *This class facilitates the use of the parray member of
 *CComVariant for storage of binary data. */

//Example of use:

int main(int argc, char* argv[])
{
  char * szText=  "this is the text\0and more";
  CSafeArrayVariant vSafeAr(szText,25);
  //vSafeAr now is a blob (VT_UI1|VT_ARRAY) that can be used
  //for COM calls.
  char * szRead  = (char*)vSafeAr;
  char * szExtra = szRead + 17;
  return 0;
}

class CSafeArrayVariant : public CComVariant
{
public:
  inline ~CSafeArrayVariant()
  {

  }
  inline void Clear()
  {
    if((vt & VT_ARRAY) && parray)
    {
      SafeArrayDestroy(parray);
      parray = 0;
    }
  }
  inline void ReadBytes(BYTE * pBytes,int nLength)
  {
    Clear();
    vt = VT_ARRAY|VT_UI1;
    BYTE * pBytes2 = 0;
    m_nLength = (nLength > 0) ? nLength : 0;
    parray = SafeArrayCreateVector(VT_UI1,1,m_nLength + 1);
    SafeArrayAccessData(parray,(void**)&pBytes2);
    memcpy(pBytes2,pBytes,m_nLength);
    pBytes2[m_nLength] = 0;
    SafeArrayUnaccessData(parray);
  }
  inline void ReadBytes(char* szText,int nLength)
  {
    BYTE * pBytes = (BYTE*)szText;
    m_nLength = nLength;
    ReadBytes(pBytes,nLength);
  }
  inline operator += (CSafeArrayVariant &var)
  {
    int nAddedLength = var.GetLength();
    if(nAddedLength)
    {
      int nNewLength = m_nLength + nAddedLength;
      BYTE * pBytes = (BYTE*)_alloca(nNewLength);
      if(m_nLength)
      memcpy(pBytes,(char*)(*this),m_nLength);
      memcpy(pBytes + m_nLength,(char*)var,nAddedLength);
      ReadBytes(pBytes,nNewLength);
    }
  }
  inline CSafeArrayVariant()
  {
    parray    = 0;
    m_nLength = 0;
  }
  inline CSafeArrayVariant(char * szBytes,int nLength)
  {
    ReadBytes(szBytes,nLength);
  }
  inline CSafeArrayVariant(const CComVariant& var)
  {
    parray   = 0;
    operator =(var);
  }


  inline operator = (VARIANT var)
  {
    m_nLength = 0;
    if(var.vt == (VT_ARRAY|VT_UI1))
    {
      long lBound,uBound;
      SafeArrayGetLBound(var.parray,1,&lBound);
      SafeArrayGetUBound(var.parray,1,&uBound);
      m_nLength = uBound - lBound;
    }
    CComVariant::operator =(var);
  }
  inline int GetLength()const \
  {
    return m_nLength;
  }
  inline operator char*()
  {
    //use GetLength() to make sure you read everything
    if(vt == (VT_ARRAY|VT_UI1))
    {
    BYTE * pBytes = 0;
    long lBound,uBound;
    SafeArrayGetLBound(parray,1,&lBound);
    SafeArrayGetUBound(parray,1,&uBound);
    m_nLength = uBound - lBound;
    SafeArrayAccessData(parray,(void**)&pBytes);
    char * szVal = (char*)pBytes;
    SafeArrayUnaccessData(parray);
    return szVal;
    }
    USES_CONVERSION;
    return W2A(bstrVal);
  }
protected:
int m_nLength;
};

#endif


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds