Visual c++ 在C+;中WinRT HttpBufferContent和非托管内存之间转换+;cx

Visual c++ 在C+;中WinRT HttpBufferContent和非托管内存之间转换+;cx,visual-c++,windows-runtime,unmanaged,c++-cx,winrt-component,Visual C++,Windows Runtime,Unmanaged,C++ Cx,Winrt Component,作为WinRT C++cx组件的一部分,使用Windows::Web::Http::HttpBufferContent来回转换字节的非托管缓冲区(例如表示为std::string)的最有效方法是什么 这就是我的结局,但它似乎不是很理想: std::string至HttpBufferContent: std::string m_body = ...; auto writer = ref new DataWriter(); writer->WriteBytes(ArrayReference&l

作为WinRT C++cx组件的一部分,使用
Windows::Web::Http::HttpBufferContent
来回转换字节的非托管缓冲区(例如表示为
std::string
)的最有效方法是什么

这就是我的结局,但它似乎不是很理想:

std::string
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());
更新:这是我最终使用的版本(您可以从
Windows::Storage::Streams::IBuffer^
创建
HttpBufferContent^
):

void IBuffer字符串(IBuffer^buffer,std::string&string){
数组^Array=nullptr;
CryptographicBuffer::CopyToByteArray(缓冲区和数组);//TODO:避免复制
赋值(重新解释(数组->数据),数组->长度);
}
IBuffer^StringToIBuffer(常量标准::字符串和字符串){
auto-array=ArrayReference(reinterpret_cast(const_cast(string.data())),string.length());
返回CryptographicBuffer::CreateFromByteArray(数组);
}

我认为您当前将HttpBufferContent转换为std::string的方法中至少制作了一个不必要的数据副本,您可以通过直接访问IBuffer数据来改进这一点,请参见此处接受的答案:

我认为您当前将HttpBufferContent转换为std::string的方法中至少制作了一个不必要的数据副本,您可以通过直接访问IBuffer数据来改进这一点,请参见此处接受的答案:

我认为最好使用智能指针(无需内存管理):

#包括
#包括
#包括
使用名称空间Windows::Storage::Streams;
使用名称空间Microsoft::WRL;
伊布弗缓冲液;
ComPtr字节访问;
reinterpret_cast(buffer)->查询接口(IID_PPV_参数(&byte_访问));
std::unique\u ptr raw\u buffer=std::make\u unique(buffer->Length);
字节访问->缓冲区(原始缓冲区.get());
std::string str(reinterpret_cast(raw_buffer.get());//只有一份

我认为最好使用智能指针(无需内存管理):

#包括
#包括
#包括
使用名称空间Windows::Storage::Streams;
使用名称空间Microsoft::WRL;
伊布弗缓冲液;
ComPtr字节访问;
reinterpret_cast(buffer)->查询接口(IID_PPV_参数(&byte_访问));
std::unique\u ptr raw\u buffer=std::make\u unique(buffer->Length);
字节访问->缓冲区(原始缓冲区.get());
std::string str(reinterpret_cast(raw_buffer.get());//只有一份
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();
}
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);
}
#include <wrl.h>
#include <robuffer.h>
#include <memory>
using namespace Windows::Storage::Streams;
using namespace Microsoft::WRL;

IBuffer^ buffer;
ComPtr<IBufferByteAccess> byte_access;
reinterpret_cast<IInspectable*>(buffer)->QueryInterface(IID_PPV_ARGS(&byte_access));
std::unique_ptr<byte[]> raw_buffer = std::make_unique<byte[]>(buffer->Length);
byte_access->Buffer(raw_buffer.get());
std::string str(reinterpret_cast<char*>(raw_buffer.get())); // just 1 copy