I have an object that implements the interface Windows::Storage::Streams::IBuffer
, and I want to get an array of bytes out of it, however while looking at the d
Use the extension method like a static method:
IBuffer *buffer;
array<unsigned char>^ result= System::Runtime::InteropServices::WindowsRuntime::WindowsRuntimeBufferExtensions::ToArray(buffer);
This is a C++/CX version:
std::vector<unsigned char> getData( ::Windows::Storage::Streams::IBuffer^ buf )
{
auto reader = ::Windows::Storage::Streams::DataReader::FromBuffer(buf);
std::vector<unsigned char> data(reader->UnconsumedBufferLength);
if ( !data.empty() )
reader->ReadBytes(
::Platform::ArrayReference<unsigned char>(
&data[0], data.size()));
return data;
}
For more information see Array and WriteOnlyArray (C++/CX).
Also check this method:
IBuffer -> Platform::Array
CryptographicBuffer.CopyToByteArray
Platform::Array -> IBuffer
CryptographicBuffer.CreateFromByteArray
As a side note, if you want to create Platform::Array
from simple C++ array you could use Platform::ArrayReference
, for example:
char* c = "sdsd";
Platform::ArrayReference<unsigned char> arraywrapper((unsigned char*) c, sizeof(c));
You can use IBufferByteAccess, through exotic COM casts:
byte* GetPointerToPixelData(IBuffer^ buffer)
{
// Cast to Object^, then to its underlying IInspectable interface.
Object^ obj = buffer;
ComPtr<IInspectable> insp(reinterpret_cast<IInspectable*>(obj));
// Query the IBufferByteAccess interface.
ComPtr<IBufferByteAccess> bufferByteAccess;
ThrowIfFailed(insp.As(&bufferByteAccess));
// Retrieve the buffer data.
byte* pixels = nullptr;
ThrowIfFailed(bufferByteAccess->Buffer(&pixels));
return pixels;
}
Code sample copied from http://cm-bloggers.blogspot.fi/2012/09/accessing-image-pixel-data-in-ccx.html
This should work with WinRT extensions:
// Windows::Storage::Streams::DataReader
// buffer is assumed to be of type Windows::Storage::Streams::IBuffer
// BYTE = unsigned char
DataReader^ reader = DataReader::FromBuffer(buffer);
BYTE *extracted = new BYTE[buffer->Length];
// NOTE: This will read directly into the allocated "extracted" buffer
reader->ReadBytes(Platform::ArrayReference<BYTE>(extracted, buffer->Length));
// ... do something with extracted...
delete [] extracted; // don't forget to free the space
Since this question is tagged c++, here's a solution using C++/WinRT. It essentially does the same as this answer under the hood, but is way more accessible. The (undocumented) data()
helper on the IBuffer
projection does all the heavy lifting:
uint8_t* GetPointerToPixelData(::winrt::Windows::Storage::Streams::IBuffer const& buffer)
{
return buffer.data();
}
There is unfortunately no official documentation (yet), and I only stumbled across this in the sample code for the WritableBitmap.PixelBuffer property (make sure to select "C++/WinRT" from the language dropdown at the top right).
An identical solution (querying for the IBufferByteAccess
interface) is also available from that documentation entry when selecting "C++/CX" from the language dropdown.