EliteTUM
EliteTUM

Reputation: 705

Visual C++ (.NET 4.0) - Serializing/Copy custom struct to Byte-Array for sending via NetworkStream

I am working on a TCP-Communication with another person's program. For proper Communication he defined a Header-Struct for the Messages sent via TCP. It is defined something as following:

typedef struct
{
    uint16_t  number1;                 
    uint16_t  number2;
    char      name1[64];
    char      name2[64];
    uint32_t  size;
} headerStruct;

The struct is somehow filled, most importantly headerStruct.size sets the number of bytes following the header and is the size of the messages to be sent.

I now tried to serialize the header like this:

headerStruct sourceHeader;
// ... filling Header-Struct here ...

array<Byte>^ result = gcnew array<Byte>(520);  // I tried sizeof(headerStruct) instead of '520', but then I get errors
double allreadyCopied = 0;

// serialize uint16_t 'number1'
array<Byte>^ array1 = BitConverter::GetBytes( sourceHeader.number1 );
Array::Copy(array1 , 0, result, allreadyCopied, array1 ->Length);
allreadyCopied += array1 ->Length;

// serialize uint16_t 'number2'
array<Byte>^ array2 = BitConverter::GetBytes( sourceHeader.number2 );
Array::Copy(array2 , 0, result, allreadyCopied, array2->Length);
allreadyCopied += array2->Length;

// serialize char-Array 'name1'    
for (int i = 0; i < sizeof(sourceHeader.name1); i++) 
{
    array<Byte>^ arrayName1 = BitConverter::GetBytes( sourceHeader.name1[i] );
    Array::Copy(arrayName1 , 0, result, allreadyCopied, arrayName1->Length);
    allreadyCopied += arrayName1->Length;
}

// serialize char-Array 'name2'       
for (int i = 0; i < sizeof(sourceHeader.name2); i++) 
{
    array<Byte>^ arrayName2 = BitConverter::GetBytes( sourceHeader.name2[i] );
    Array::Copy(arrayName2 , 0, result, allreadyCopied, arrayName2->Length);
    allreadyCopied += arrayName2->Length;
}

// serialize uint32_t 'size'        
array<Byte>^ arraySize = BitConverter::GetBytes( sourceHeader.size);
Array::Copy(arraySize, 0, result, allreadyCopied, arraySize->Length);
allreadyCopied += arraySize->Length;

Now this seems to work for me, BUT:

  1. I tried using sizeof(headerStruct) instead of the hardcoded value 520 for the size if the result-array. I just "calculated" it with the variable allreadyCopied. But why are they different? Where's my error here?

  2. This method is surely not very elegant. Is there a better way to do this? I tried the Marshal-Class but did not manage to get it working!

  3. Somehow it feels like this approach is wrong. Is this the right way or am I completely wrong here?

Any help is appreciated a lot! Thanks!

Edit:

OK just checked:

sizeof(headerStruct);       // = 136;
sizeof(char);               // = 1;
sizeof(sourceHeader.name1); // = 64;

BUT

arrayName1->Length;         // = 4;

Why? Very confusing: MSDN (link here) says that the returned Array should be of length 2, not 4.

Upvotes: 1

Views: 1500

Answers (2)

EliteTUM
EliteTUM

Reputation: 705

OK, I googled a lot and read some articles on MSDN. I am now doing it like this:

headerStruct sourceHeader;
// ... filling Header-Struct here ..

// First Create all arrays, for every element of the struct one Array
array< Byte >^ array1 = gcnew array< Byte >(sizeof(sourceHeader.number1));
array< Byte >^ array2 = gcnew array< Byte >(sizeof(sourceHeader.number2));
array< Byte >^ arrayName1 = gcnew array< Byte >(sizeof(sourceHeader.name1));
array< Byte >^ arrayName2 = gcnew array< Byte >(sizeof(sourceHeader.name2));
array< Byte >^ arraySize = gcnew array< Byte >(sizeof(sourceHeader.size));

// Let the Marshall Class copy those native Types (by casting there Pointers to a IntPtr)
Marshal::Copy( (IntPtr)(&sourceHeader.number1), array1, 0, sizeof(sourceHeader.number1) );
Marshal::Copy( (IntPtr)(&sourceHeader.number2), array2, 0, sizeof(sourceHeader.number2) );
Marshal::Copy( (IntPtr)(&sourceHeader.name1), arrayName1, 0, sizeof(sourceHeader.name1) );
Marshal::Copy( (IntPtr)(&sourceHeader.name2), arrayName2, 0, sizeof(sourceHeader.name2) );
Marshal::Copy( (IntPtr)(&sourceHeader.size), arraySize, 0, sizeof(sourceHeader.size) );

// Next we create the Array in Which all Data shall be copied alltogether
array<Byte>^ allInOne = gcnew array<Byte>(sizeof(headerStruct));
int allreadyCopied = 0;

// Copy those arrays into the "Big" Array (watch out for the right order!)
Array::Copy(array1, 0, allInOne, allreadyCopied, array1->Length);
allreadyCopied += array1->Length;
Array::Copy(array2, 0, allInOne, allreadyCopied, array2->Length);
allreadyCopied += array2->Length;
Array::Copy(arrayName1, 0, allInOne, allreadyCopied, arrayName1->Length);
allreadyCopied += arrayName1->Length;
Array::Copy(arrayName2, 0, allInOne, allreadyCopied, arrayName2->Length);
allreadyCopied += arrayName2->Length;
Array::Copy(arraySize, 0, allInOne, allreadyCopied, arraySize->Length);
allreadyCopied += arraySize->Length;

// Now let's Test if it worked correctly by converting back the ByteArray to a Struct
pin_ptr<unsigned char> p1 = &allInOne[0];
unsigned char* p2 = p1;
headerStruct myHeader = *reinterpret_cast<headerStruct*>(p2);

Works for me :) Hope this approach is OK the way I do it. I used the Infos and Examples on following sites for this solution:

  1. MSDN Article 1
  2. MSDN Article 2

This should work for most Custom-Structs using native types, right? Thanks @ Hans Passant for pointing me into the right direction :)

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941724

You are getting yourself into trouble my mixing native (unmanaged) and managed types. The sizeof(headerStruct) doesn't work because you forgot that you add extra bytes after it. The arrayName1->Length is 4 because you are passing a native char to GetBytes(). The compiler promotes it to the next compatible managed type, Int32. Only a Char is 2 bytes, note the capital C. Equivalent to wchar_t in native code.

Sending binary data like this across a network is usually a mistake. You hope that the other end uses the same kind of compiler and same kind of endian-ness. Which is fine when you control both ends but it sounds like you don't. A generic works-everywhere format is XML. That's trivial in .NET but not for a native type. You'd have to write a wrapper for the struct that's a ref class and uses managed types. Also the reason why you have trouble with the Marshal class.

Upvotes: 1

Related Questions