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: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds