Table of Contents

Class CoreWebView2SharedBuffer

Namespace
Microsoft.Web.WebView2.Core
Assembly
Microsoft.Web.WebView2.Core.dll

A shared memory based buffer object that is created by CreateSharedBuffer(ulong). The object is presented to script as ArrayBuffer when posted to script with PostSharedBufferToScript(CoreWebView2SharedBuffer, CoreWebView2SharedBufferAccess, string).

public class CoreWebView2SharedBuffer : IDisposable
Inheritance
CoreWebView2SharedBuffer
Implements
Inherited Members

Properties

Buffer

The raw memory address of the buffer.

public nint Buffer { get; }

Property Value

nint

Remarks

You can cast it to pointer to real data types like byte* to access the memory from unsafe code region. Normal app should use OpenStream() to get a stream object to access the buffer.

FileMappingHandle

The file mapping handle of the shared memory of the buffer.

public SafeHandle FileMappingHandle { get; }

Property Value

SafeHandle

Remarks

Normal app should use OpenStream() to get a stream object to access the buffer. For advanced scenarios, you could use native file mapping APIs to obtain other views or duplicate this handle to another application process and create a view from the duplicated handle in that process to access the buffer from that separate process.

Size

The size of the shared buffer in bytes.

public ulong Size { get; }

Property Value

ulong

Methods

Close()

Release the backing shared memory.

public void Close()

Remarks

The application should call Close or Dispose() when no access to the buffer is needed any more, to ensure that the underlying resources are released timely even if the shared buffer object itself is not released due to some leaked reference. After the shared buffer is closed, the buffer address and file mapping handle previously obtained becomes invalid and cannot be used anymore. Properties of the object will also be unaccessible. Operations like Read or Write on the stream objects returned from OpenStream() will also fail. PostSharedBufferToScript(CoreWebView2SharedBuffer, CoreWebView2SharedBufferAccess, string) will also fail. The failures will be represented as COMException with error code of RO_E_CLOSED.

The script code should call chrome.webview.releaseBuffer with the shared buffer as the parameter to release underlying resources as soon as it does not need access the shared buffer any more. When script tries to access the buffer after calling chrome.webview.releaseBuffer, JavaScript TypeError exception will be raised complaining about accessing a detached ArrayBuffer, the same exception when trying to access a transferred ArrayBuffer.

Closing the buffer object on native side doesn't impact access from Script and releasing the buffer from script doesn't impact access to the buffer from native side. The underlying shared memory will be released by the OS when both native and script side release the buffer.

Dispose()

public void Dispose()

Dispose(bool)

Protected implementation of Dispose pattern.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

OpenStream()

Get an stream object that can be used to access the shared buffer.

public Stream OpenStream()

Returns

Stream