Table of Contents

Class ComBstr

Namespace
CsCom.Runtime
Assembly
CsCom.Runtime.dll

A BSTR, whose characters are accessed in place rather than copied to a string. It also keeps the exact length in bytes of BSTRs that hold binary data.

[NativeMarshalling(typeof(ComBstrMarshaller))]
public sealed class ComBstr : IDisposable
Inheritance
ComBstr
Implements
Inherited Members

Remarks

An instance that owns its BSTR frees it when it is disposed or finalized; a view of a BSTR owned by someone else does not. When it is passed to COM methods, the ownership follows the COM rules: [in] strings stay owned by the caller, returned strings are owned by the receiver, and a string replaced by the callee of an [in, out] parameter is detached. The strings received by COM objects implemented in .NET are views that are valid during the call; an owned string returned by them is detached and its ownership is transferred to the caller.

Properties

ByteLength

Gets the number of bytes, which can be odd for BSTRs that hold binary data.

public int ByteLength { get; }

Property Value

int

Length

Gets the number of characters.

public int Length { get; }

Property Value

int

OwnsString

Gets a value indicating whether the instance frees the BSTR when it is disposed.

public bool OwnsString { get; }

Property Value

bool

Pointer

Gets the BSTR pointer.

public nint Pointer { get; }

Property Value

nint

Exceptions

ObjectDisposedException

The instance was disposed or detached.

Methods

AsBytes()

Gets the bytes, which are valid until the instance is disposed. An instance that owns its BSTR frees it when it is finalized, so the instance must be kept reachable while the span is used, e.g. by disposing it afterwards.

public ReadOnlySpan<byte> AsBytes()

Returns

ReadOnlySpan<byte>

AsSpan()

Gets the characters, which are valid until the instance is disposed. An instance that owns its BSTR frees it when it is finalized, so the instance must be kept reachable while the span is used, e.g. by disposing it afterwards.

public ReadOnlySpan<char> AsSpan()

Returns

ReadOnlySpan<char>

Create(ReadOnlySpan<char>)

Creates a BSTR with the characters of a string.

public static ComBstr Create(ReadOnlySpan<char> value)

Parameters

value ReadOnlySpan<char>

Returns

ComBstr

CreateFromBytes(ReadOnlySpan<byte>)

Creates a BSTR that holds binary data.

public static ComBstr CreateFromBytes(ReadOnlySpan<byte> value)

Parameters

value ReadOnlySpan<byte>

Returns

ComBstr

Detach()

Gives up the BSTR without freeing it, e.g. to return it to a COM caller. The instance cannot be used afterwards.

public nint Detach()

Returns

nint

The BSTR pointer, which is owned by the caller if the instance owned it.

Dispose()

Frees the BSTR if it is owned. The instance cannot be used afterwards.

public void Dispose()

~ComBstr()

Frees the BSTR if it is owned and was not disposed.

protected ~ComBstr()

FromPointer(nint, bool)

Wraps a BSTR pointer, e.g. a BSTR field of a structure.

public static ComBstr? FromPointer(nint pointer, bool ownsString)

Parameters

pointer nint

The BSTR pointer.

ownsString bool

Whether the instance frees the BSTR when it is disposed.

Returns

ComBstr

The wrapper, or null if pointer is 0, which COM treats as an empty string.

ToString()

Copies the characters to a string.

public override string ToString()

Returns

string

Exceptions

ObjectDisposedException

The instance was disposed or detached.