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
Length
Gets the number of characters.
public int Length { get; }
Property Value
OwnsString
Gets a value indicating whether the instance frees the BSTR when it is disposed.
public bool OwnsString { get; }
Property Value
Pointer
Gets the BSTR pointer.
public nint Pointer { get; }
Property Value
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
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
Create(ReadOnlySpan<char>)
Creates a BSTR with the characters of a string.
public static ComBstr Create(ReadOnlySpan<char> value)
Parameters
valueReadOnlySpan<char>
Returns
CreateFromBytes(ReadOnlySpan<byte>)
Creates a BSTR that holds binary data.
public static ComBstr CreateFromBytes(ReadOnlySpan<byte> value)
Parameters
valueReadOnlySpan<byte>
Returns
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
BSTRpointer, 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
pointernintThe
BSTRpointer.ownsStringboolWhether the instance frees the
BSTRwhen it is disposed.
Returns
ToString()
Copies the characters to a string.
public override string ToString()
Returns
Exceptions
- ObjectDisposedException
The instance was disposed or detached.