NativeBuffer.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include <wrl.h>
  3. #include <wrl/implements.h>
  4. #include <windows.storage.streams.h>
  5. #include <robuffer.h>
  6. #include <stdint.h>
  7. class NativeBuffer :
  8. public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::WinRtClassicComMix>,
  9. ABI::Windows::Storage::Streams::IBuffer,
  10. Windows::Storage::Streams::IBufferByteAccess>
  11. {
  12. public:
  13. virtual ~NativeBuffer()
  14. {
  15. }
  16. STDMETHODIMP RuntimeClassInitialize(byte *buffer, UINT totalSize)
  17. {
  18. m_length = totalSize;
  19. m_buffer = buffer;
  20. return S_OK;
  21. }
  22. STDMETHODIMP Buffer(byte **value)
  23. {
  24. *value = m_buffer;
  25. return S_OK;
  26. }
  27. STDMETHODIMP get_Capacity(UINT32 *value)
  28. {
  29. *value = m_length;
  30. return S_OK;
  31. }
  32. STDMETHODIMP get_Length(UINT32 *value)
  33. {
  34. *value = m_length;
  35. return S_OK;
  36. }
  37. STDMETHODIMP put_Length(UINT32 value)
  38. {
  39. m_length = value;
  40. return S_OK;
  41. }
  42. static Windows::Storage::Streams::IBuffer^ GetIBuffer(byte *buffer, uint32_t totalSize)
  43. {
  44. Microsoft::WRL::ComPtr<NativeBuffer> nativeBuffer;
  45. Microsoft::WRL::Details::MakeAndInitialize<NativeBuffer>(&nativeBuffer, buffer, totalSize);
  46. auto obj = reinterpret_cast<IInspectable*>(nativeBuffer.Get());
  47. return reinterpret_cast<Windows::Storage::Streams::IBuffer^>(obj);
  48. }
  49. private:
  50. UINT32 m_length;
  51. byte *m_buffer;
  52. };