I'm setting my first steps in COM communication. I have an application, written in C#, using a byte array, and that is working fine.
When I try to do the exact same thing in C++, it does not work.
I have taken two Wireshark captures, where the differences can be seen clearly.
This is a screenshot of the good situation:

This is a screenshot of the bad situation:

You can clearly see the two cccc bytes, added to the C++ data.
For your information: in the C# application I'm using the following source code to convert a byte array into an object for being sent:
byte[] requestCounters = new byte[] { 0xFF, 0xFF };
dataCounters = (object) requestCounters;
In the C++ application (the failing one), it goes as follows:
BYTE bData_Counters[] = {0xFF, 0xFF};
_variant_t data_Counters = from_ByteArray(bData_Counters); // new byte[] { 0xFF, 0xFF };
The from_ByteArray() function looks as follows:
_variant_t from_ByteArray(BYTE input[])
{
long dataCount = sizeof(input) / sizeof(BYTE);
// Create a SAFEARRAY to hold the bytes
SAFEARRAYBOUND sabound;
sabound.lLbound = 0;
sabound.cElements = dataCount;
SAFEARRAY* psa = SafeArrayCreate(VT_UI1, 1, &sabound); // VT_UI1 = unsigned byte
// Copy the bytes into the SAFEARRAY
void* pArrayData = nullptr;
SafeArrayAccessData(psa, &pArrayData);
memcpy(pArrayData, input, dataCount);
SafeArrayUnaccessData(psa);
// Wrap the SAFEARRAY in a VARIANT (via _variant_t)
_variant_t data;
data.vt = VT_ARRAY | VT_UI1;
data.parray = psa;
return data;
}
Why is the C++ way adding two bytes to the data?
Does anybody have an idea?
BYTE input[]have many tricky and misleading properties. That's why yourlong dataCountformula is wrong. Consider using a C++ solution, such asstd::span<Byte>.sizeof(input) / sizeof(BYTE)issizeof(BYTE*) / sizeof(BYTE), a constant value...sizeofto determine the size of an array. When it would work thenstd::sizedoes too, and when it doesnt it does silently the wrong thing whilestd::sizecreates a lovely error_variant_tif you want to give theVARIANTback as the smart class will destroy it automatically when out of scope, just useVARIANTor dodata.Detach(). PS: use one linerSafeArrayCreateVectorExlearn.microsoft.com/en-us/windows/win32/api/oleauto/… it's much easier to use for what you need to do