Search code examples
visual-c++windows-runtimeunmanagedc++-cxwinrt-component

Converting between WinRT HttpBufferContent and unmanaged memory in C++cx


As part of a WinRT C++cx component, what's the most efficient way to convert an unmanaged buffer of bytes (say expressed as a std::string) back and forth with a Windows::Web::Http::HttpBufferContent?

This is what I ended up with, but it doesn't seem very optimal:

std::string to HttpBufferContent:

std::string m_body = ...;
auto writer = ref new DataWriter();
writer->WriteBytes(ArrayReference<unsigned char>(reinterpret_cast<unsigned char*>(const_cast<char*>(m_body.data())), m_body.length()));
auto content = ref new HttpBufferContent(writer->DetachBuffer());

HttpBufferContent to std::string:

HttpBufferContent^ content = ...
auto operation = content->ReadAsBufferAsync();
auto task = create_task(operation);
if (task.wait() == task_status::completed) {
    auto buffer = task.get();
    size_t length = buffer->Length;
    if (length > 0) {
        unsigned char* storage = static_cast<unsigned char*>(malloc(length));
        DataReader::FromBuffer(buffer)->ReadBytes(ArrayReference<unsigned char>(storage, length));
        auto m_body = std::string(reinterpret_cast<char*>(storage), length);
        free(storage);
    }
} else {
    abort();
}

UPDATE: Here's the version I ended up using (you can trivially create a HttpBufferContent^ from an Windows::Storage::Streams::IBuffer^):

void IBufferToString(IBuffer^ buffer, std::string& string) {
    Array<unsigned char>^ array = nullptr;
    CryptographicBuffer::CopyToByteArray(buffer, &array);  // TODO: Avoid copy
    string.assign(reinterpret_cast<char*>(array->Data), array->Length);
}

IBuffer^ StringToIBuffer(const std::string& string) {
    auto array = ArrayReference<unsigned char>(reinterpret_cast<unsigned char*>(const_cast<char*>(string.data())), string.length());
    return CryptographicBuffer::CreateFromByteArray(array);
}

Solution

  • I think you are making at least one unnecessary copy of your data in your current approach for HttpBufferContent to std::string, you could improve this by accessing the IBuffer data directly, see the accepted answer here: Getting an array of bytes out of Windows::Storage::Streams::IBuffer