Table of Contents

Class ComDispatch

Namespace
CsCom.Runtime
Assembly
CsCom.Runtime.dll

Resolves and invokes the members of IDispatch interfaces by name and dispatch identifier, and reads their type information: the late binding that the generated dispinterface wrappers and dynamic objects build on.

public static class ComDispatch
Inheritance
ComDispatch
Inherited Members

Methods

CreateInvocationException(int, int, string?, DispatchFlags, int, int)

Creates the exception of a failed invocation, as Invoke(IDispatch, int, DispatchFlags, ReadOnlySpan<object?>, ReadOnlySpan<int>, Span<object?>, ReadOnlySpan<DispatchArgumentKind>, string?) throws it: a COMException that names the member and the argument for the common errors of IDispatch::Invoke, otherwise the exception of the HRESULT.

public static Exception CreateInvocationException(int hr, int dispId, string? memberName, DispatchFlags flags, int argumentCount, int argumentError)

Parameters

hr int

The HRESULT returned by TryInvoke(IDispatch, int, DispatchFlags, ReadOnlySpan<object?>, ReadOnlySpan<int>, Span<object?>, ReadOnlySpan<DispatchArgumentKind>, out object?, out int).

dispId int

The dispatch identifier of the member.

memberName string

The name of the member, if known.

flags DispatchFlags

The kind of the invocation.

argumentCount int

The number of arguments.

argumentError int

The index of the argument that caused the error, or -1.

Returns

Exception

GetDispIds(IDispatch, ReadOnlySpan<string>, Span<int>)

Maps a member name and, optionally, the names of its parameters to dispatch identifiers (IDispatch::GetIDsOfNames).

public static int GetDispIds(IDispatch dispatch, ReadOnlySpan<string> names, Span<int> ids)

Parameters

dispatch IDispatch

The interface.

names ReadOnlySpan<string>

The name of the member, followed by the names of parameters.

ids Span<int>

Receives the dispatch identifiers; Unknown for the unknown names.

Returns

int

The HRESULT; DISP_E_UNKNOWNNAME when one of the names is unknown.

GetEventInterfaces(IDispatch)

Gets the source dispinterfaces of the coclass of an object, the default one first: the coclass is given by IProvideClassInfo, or is the first coclass of the type library whose default interface is the interface of IDispatch::GetTypeInfo. Dual and vtable source interfaces are not included.

public static IReadOnlyList<EventInterface> GetEventInterfaces(IDispatch dispatch)

Parameters

dispatch IDispatch

Returns

IReadOnlyList<EventInterface>

GetMemberKinds(IDispatch, int)

Gets how the type information of an object describes a member: as a method, as a property that can be read, and whether the getter of the property takes arguments. Late-bound callers that read a member can then return something to call or to index rather than invoke it.

public static DispatchMemberKinds GetMemberKinds(IDispatch dispatch, int dispId)

Parameters

dispatch IDispatch
dispId int

Returns

DispatchMemberKinds

The kinds, or None for an unknown member and if the object provides no type information.

GetMemberName(IDispatch, int)

Gets the name of a member of an object from its type information, e.g. the name of the default member (Value), which GetDispIds(IDispatch, ReadOnlySpan<string>, Span<int>) needs to resolve the names of its parameters.

public static string? GetMemberName(IDispatch dispatch, int dispId)

Parameters

dispatch IDispatch
dispId int

Returns

string

The name, or null if the object provides no type information or the type has no such member.

GetPropertyNames(IDispatch)

Gets the names of the properties of an object that can be read without arguments, including those of the base interfaces, except for restricted and hidden ones.

public static IReadOnlyList<string> GetPropertyNames(IDispatch dispatch)

Parameters

dispatch IDispatch

Returns

IReadOnlyList<string>

The names, or none if the object provides no type information.

GetTypeName(IDispatch)

Gets the name of the type of an object from its type information (IDispatch::GetTypeInfo), like VB's TypeName, e.g. the name of the default interface of its class.

public static string? GetTypeName(IDispatch dispatch)

Parameters

dispatch IDispatch

Returns

string

The name, or null if the object provides no type information.

Invoke(IDispatch, int, DispatchFlags, ReadOnlySpan<object?>, ReadOnlySpan<int>, Span<object?>, ReadOnlySpan<DispatchArgumentKind>, string?)

Invokes a member and throws an exception if the invocation fails.

public static object? Invoke(IDispatch dispatch, int dispId, DispatchFlags flags, ReadOnlySpan<object?> args, ReadOnlySpan<int> namedDispIds = default, Span<object?> output = default, ReadOnlySpan<DispatchArgumentKind> byRef = default, string? memberName = null)

Parameters

dispatch IDispatch

The interface to invoke the member on.

dispId int

The dispatch identifier of the member.

flags DispatchFlags

The kind of the invocation.

args ReadOnlySpan<object>

The arguments, in the declaration order; the named arguments follow the positional ones.

namedDispIds ReadOnlySpan<int>

The dispatch identifiers of the trailing named arguments. A property put without named arguments passes its last argument, the value, as PropertyPut; with named arguments, the value must be the first of them, with PropertyPut as its identifier.

output Span<object>

Receives the new values of the by-reference arguments; usually the same span as args. It must have an element for each argument when byRef is not empty.

byRef ReadOnlySpan<DispatchArgumentKind>

For each argument, how it is passed. Empty when all of them are passed by value.

memberName string

The name of the member for error messages, if known.

Returns

object

The result of the invocation.

Exceptions

COMException

The invocation failed or the COM object raised an exception.

TryInvoke(IDispatch, int, DispatchFlags, ReadOnlySpan<object?>, ReadOnlySpan<int>, Span<object?>, ReadOnlySpan<DispatchArgumentKind>, out object?, out int)

Invokes a member and returns the HRESULT. Exceptions raised by the COM object (DISP_E_EXCEPTION) are still thrown.

public static int TryInvoke(IDispatch dispatch, int dispId, DispatchFlags flags, ReadOnlySpan<object?> args, ReadOnlySpan<int> namedDispIds, Span<object?> output, ReadOnlySpan<DispatchArgumentKind> byRef, out object? result, out int argumentError)

Parameters

dispatch IDispatch

The interface to invoke the member on.

dispId int

The dispatch identifier of the member.

flags DispatchFlags

The kind of the invocation.

args ReadOnlySpan<object>

The arguments, in the declaration order; the named arguments follow the positional ones.

namedDispIds ReadOnlySpan<int>

The dispatch identifiers of the trailing named arguments.

output Span<object>

Receives the new values of the by-reference arguments; usually the same span as args.

byRef ReadOnlySpan<DispatchArgumentKind>

For each argument, how it is passed. Empty when all of them are passed by value.

result object

The result of the invocation.

argumentError int

The index in args of the argument that caused a DISP_E_TYPEMISMATCH or DISP_E_PARAMNOTFOUND error, or -1.

Returns

int

The HRESULT returned by IDispatch::Invoke.

Exceptions

COMException

The COM object raised an exception.