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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Flash technology is becoming more prominent in the storage industry. Offering superior speed and reliability when compared to traditional hard disk drives – flash storage is a flexible and increasingly cost-effective technology that can be used to optimize enterprise storage environments. This ebook explores the many uses and benefits of flash storage technology in the enterprise. Check it out to discover and learn all you need to: Optimize storage performance Leverage server flash as storage cache …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds