I am working with a sample UWP C++/CX program that creates two UDP network communication threads which are sending data to each other using Windows::Storage::Streams::DataWriter
and Windows::Storage::Streams::DataReader
while updating a displayed window showing the data going back and forth.
The initial implementation using Platform::String
variables along with DataWriter->WriteString()
worked fine. However now I want to implement a binary protocol that has variable length buffers containing various types of information.
DataReader->ReadBuffer()
and DataWriter->WriteBuffer()
requires a Windows::Storage::Streams::Buffer
.
To access the Buffer returned by ReadBuffer()
I am using a function whose source if found on the web at Obtaining pointers to data buffers (C++/CX) which is similar to an answer in Getting an array of bytes out of Windows::Storage::Streams::IBuffer
#include <robuffer.h>
byte* GetPointerToPixelData(IBuffer^ pixelBuffer, unsigned int *length)
{
if (length != nullptr)
{
*length = pixelBuffer->Length;
}
// Query the IBufferByteAccess interface.
Microsoft::WRL::ComPtr<IBufferByteAccess> bufferByteAccess;
reinterpret_cast<IInspectable*>(pixelBuffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));
// Retrieve the buffer data.
byte* pixels = nullptr;
bufferByteAccess->Buffer(&pixels);
return pixels;
}
However I have been unable to find out how to copy a native struct into a Buffer so that it can be sent out using DataWriter->WriteBuffer()
.
If I have some struct of binary data, how can I copy the contents of this struct into the output Windows::Storage::Streams::Buffer
for use with DataWriter->WriteBuffer()
?