Introduction
When writing ATL components, I have found that one of the hardest things for Windows
programmers to overcome is the dependency on the MFC library. For dates, strings and byte arrays, MFC
simplifies the programmer's job by handling memory allocations and type conversions within
its class encapsulation. However, with that simplification comes the price of having to
distribute the MFC DLL's with your component.
This article focuses on the issues surrounding the use of byte arrays in an ATL component.
If you dont include MFC support in your ATL component, you obviously wont have
the CByteArray available to use. I, therefore, wrote CCOMByteArray to provide the functionality
of MFC's CByteArray, but without the MFC dependency. Please note that I do not currently
include exception handling for the memory allocation functions. I am currently trapping any
memory allocation errors with the ATLASSERT macro, but these will compile away in Release mode.
This functionality along with performance improvements will be added to a future version.
The current version of the CCOMByteArray includes the following
functionality. Please note, if there are any functions in the CCOMByteArray class that
you download that are not listed in the text below, they are considered to be undocumented
and, therefore, possibly untested as well:
Constructors
CCOMByteArray()
- Constructs an empty byte array.
CCOMByteArray(BYTE*, UINT)
- Constructs a CCOMByteArray object from a pointer to a BYTE array.
CCOMByteArray(BSTR&)
- Constructs a CCOMByteArray object from a Binary String (i.e., BSTR)
CCOMString(BSTR)
- Constructs a CCOMByteArray object from a BSTR.
Attributes
GetSize() const
- Returns the size of the byte array.
GetUpperBound() const
- Returns the upper boundary (i.e., the size minus one) of the byte array.
SetSize(UINT, UINT)
- Set the size of the array along with the grow by factor.
Cleanup
RemoveAll()
- Removes all elements from the array.
FreeExtra()
- Free any extra allocated memory that is not currently used by the array.
Accessors
GetAt(UINT) const
- Returns the byte located at the specified location.
ElementAt(UINT)
- Returns a reference to the byte located at the specified location.
SetAt(BYTE, UINT)
- Sets the element to the specified byte value at the specified location.
Accessors
GetAt(UINT) const
- Returns the byte located at the specified location.
Operator[](UINT)
- Same functionality as GetAt(UINT) (See above).
ElementAt(UINT)
- Returns a reference to the byte located at the specified location.
SetAt(BYTE, UINT)
- Sets the element to the specified byte value at the specified location.
Direct Access
GetData() const
- Returns a pointer to the array contained in CCOMByteArray.
Growing the Array
Append(BYTE)
- Adds the specified byte to the existing array.
Append(const CCOMByteArray&apm;)
- Adds the specified CCOMByteArray to the existing array.
Append(BYTE*, UINT)
- Adds the array specified by the BYTE* to the existing array.
Append(BSTR&)
- Adds the specified Binary String to the existing array.
Operator+=(CCOMByteArray&)
- Same functionality as Append(CCOMByteArray&) (See above).
Operator+=(BYTE&)
- Same functionality as Append(BYTE&) (See above).
Operator+=(BSTR&)
- Same functionality as Append(BSTR&) (See above).
Move Elements Around
InsertAt(UINT, BYTE, UINT)
- Inserts the specified byte at the specified location for the specified count.
InsertAt(UINT, CCOMByteArray&)
- Inserts the specified CCOMByteArray starting at the specified location.
RemoveAt(UINT, UINT)
- Removes the element at the specified location for the specified count.