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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

Most Popular Programming Stories

More for Developers

RSS Feeds