<PackageReference Include="System.Drawing.Common" Version="10.0.0-rc.2.25502.107" />

GlobalBuffer

struct GlobalBuffer
Simple scope for writing to HGLOBAL memory.
using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Windows.Win32; using Windows.Win32.Foundation; namespace System.Private.Windows.Ole { [CompilerFeatureRequired("RefStructs")] internal ref struct GlobalBuffer { private unsafe void* _pointer; private Span<byte> _buffer; private HGLOBAL _hglobal; public HRESULT Status { [IsReadOnly] get; private set; } public unsafe void* Pointer { [IsReadOnly] get { return _pointer; } } public unsafe GlobalBuffer(HGLOBAL hglobal, uint length) { _pointer = default(void*); _buffer = default(Span<byte>); _hglobal = default(HGLOBAL); Status = HRESULT.S_OK; if (hglobal.IsNull) Status = HRESULT.E_INVALIDARG; else { _hglobal = PInvokeCore.GlobalReAlloc(hglobal, (UIntPtr)length, 2); if (_hglobal.IsNull) Status = HRESULT.E_OUTOFMEMORY; else { _pointer = PInvokeCore.GlobalLock(_hglobal); if (_pointer == null) Status = HRESULT.E_OUTOFMEMORY; _buffer = new Span<byte>(_pointer, (int)length); } } } [IsReadOnly] public Span<byte> AsSpan() { return _buffer; } [IsReadOnly] public Span<char> AsCharSpan() { return MemoryMarshal.Cast<byte, char>(_buffer); } public unsafe void Dispose() { if (!_hglobal.IsNull) { PInvokeCore.GlobalUnlock(_hglobal); _buffer = default(Span<byte>); _pointer = null; _hglobal = HGLOBAL.Null; } } } }