Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an array of bytes out of Windows::Storage::Streams::IBuffer

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 documentation this interface looks pretty useless, and the documentation does not offer any reference to any other class that could be combined with this interface to achieve my purpose. All I have found so far with google is a reference to the .Net class WindowsRuntimeBufferExtensions but I am using C++ so this is also a dead end.

Can someone give a hint on how to get an array of bytes from Windows::Storage::Streams::IBuffer in C++?

like image 320
Alam Brito Avatar asked Aug 07 '12 20:08

Alam Brito


2 Answers

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

like image 162
Berthier Lemieux Avatar answered Sep 19 '22 12:09

Berthier Lemieux


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));
like image 34
Tommy Avatar answered Sep 20 '22 12:09

Tommy