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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds