How to wrap char * buffer in WinRT IBuffer in C ++

I want to implement C ++ WinRT IBuffer that wraps a char * buffer, so I can use it with WinRT WriteAsync / ReadAsync operations that take an IBuffer ^ parameter.

EDIT 1 (clarification)

I want to avoid copying data.

+8
c ++ windows-runtime
source share
2 answers

Most likely copied from http://jeremiahmorrill.wordpress.com/2012/05/11/http-winrt-client-for-c/ , but adapted to directly transfer my own byte []:

NativeBuffer.h:

#pragma once #include <wrl.h> #include <wrl/implements.h> #include <windows.storage.streams.h> #include <robuffer.h> #include <vector> // todo: namespace class NativeBuffer : public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::WinRtClassicComMix>, ABI::Windows::Storage::Streams::IBuffer, Windows::Storage::Streams::IBufferByteAccess> { public: virtual ~NativeBuffer() { } STDMETHODIMP RuntimeClassInitialize(byte *buffer, UINT totalSize) { m_length = totalSize; m_buffer = buffer; return S_OK; } STDMETHODIMP Buffer(byte **value) { *value = m_buffer; return S_OK; } STDMETHODIMP get_Capacity(UINT32 *value) { *value = m_length; return S_OK; } STDMETHODIMP get_Length(UINT32 *value) { *value = m_length; return S_OK; } STDMETHODIMP put_Length(UINT32 value) { m_length = value; return S_OK; } private: UINT32 m_length; byte *m_buffer; }; 

To create an IBuffer:

 Streams::IBuffer ^CreateNativeBuffer(LPVOID lpBuffer, DWORD nNumberOfBytes) { Microsoft::WRL::ComPtr<NativeBuffer> nativeBuffer; Microsoft::WRL::Details::MakeAndInitialize<NativeBuffer>(&nativeBuffer, (byte *)lpBuffer, nNumberOfBytes); auto iinspectable = (IInspectable *)reinterpret_cast<IInspectable *>(nativeBuffer.Get()); Streams::IBuffer ^buffer = reinterpret_cast<Streams::IBuffer ^>(iinspectable); return buffer; } 

And a call to read data (lpBuffer is a byte []):

 Streams::IBuffer ^buffer = CreateNativeBuffer(lpBuffer, nNumberOfbytes); create_task(randomAccessStream->ReadAsync(buffer, (unsigned int)nNumberOfBytesToRead, Streams::InputStreamOptions::None)).wait(); 

I'm not sure ComPtr needs some cleanup, so any suggestions for memory management are welcome.

+9
source share

This should work:

 // Windows::Storage::Streams::DataWriter // Windows::Storage::Streams::IBuffer // BYTE = unsigned char (could be char too) BYTE input[1024] {}; DataWriter ^writer = ref new DataWriter(); writer->WriteBytes(Platform::ArrayReference<BYTE>(input, sizeof(input)); IBuffer ^buffer = writer->DetachBuffer(); 
+5
source share

All Articles