Class ComActivator
- Namespace
- CsCom.Runtime
- Assembly
- CsCom.Runtime.dll
Creates instances of COM classes and wraps them using source-generated COM interop.
public static class ComActivator
- Inheritance
-
ComActivator
- Inherited Members
Remarks
The methods throw a NotSupportedException for a type that a COM object cannot be converted to before anything else: before they look up a ProgID, create or get the object. That is an interface that is not a source-generated COM interface (GeneratedComInterfaceAttribute), or a class other than object and ComObject, e.g. a late-bound wrapper, which CreateDispatchInstance<T>(Guid, bool, ClassContext) creates.
Fields
DefaultContext
The default activation context: in-process and local servers.
public const ClassContext DefaultContext = InprocServer | LocalServer
Field Value
Methods
BindToMoniker<T>(string)
Gets the object identified by a moniker display name and returns it as T, like VB's
GetObject: e.g. the path of a document, which is opened by its application, winmgmts: for WMI or
new:Scripting.Dictionary for a new instance.
public static T BindToMoniker<T>(string name) where T : class
Parameters
namestringThe display name of the moniker.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the object, or object.
Exceptions
- COMException
The name is not a valid moniker or the object cannot be bound.
BindToMoniker<T>(string, bool)
Gets the object identified by a moniker display name, like BindToMoniker<T>(string), in a managed object
of its own if unique is set, see CreateInstance<T>(Guid, bool, ClassContext).
public static T BindToMoniker<T>(string name, bool unique) where T : class
Parameters
namestringThe display name of the moniker.
uniqueboolWhether the COM object gets a managed object of its own rather than the shared one.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the object, or object.
Exceptions
- COMException
The name is not a valid moniker or the object cannot be bound.
CreateDispatchInstance<T>(Guid, ClassContext)
Creates an instance of the COM class identified by clsid and wraps it into the late-bound wrapper T.
public static T CreateDispatchInstance<T>(Guid clsid, ClassContext context = ClassContext.InprocServer | ClassContext.LocalServer) where T : class, IDispatchWrapper<T>
Parameters
clsidGuidThe class identifier.
contextClassContextThe context in which the code that manages the newly created object will run.
Returns
- T
Type Parameters
TA late-bound wrapper of a dispinterface.
CreateDispatchInstance<T>(Guid, bool, ClassContext)
Creates an instance of the COM class identified by clsid and wraps it into the late-bound wrapper
T, with a managed object of its own if unique is set, see
CreateInstance<T>(Guid, bool, ClassContext): Release(object?) of the wrapper
releases it.
public static T CreateDispatchInstance<T>(Guid clsid, bool unique, ClassContext context = ClassContext.InprocServer | ClassContext.LocalServer) where T : class, IDispatchWrapper<T>
Parameters
clsidGuidThe class identifier.
uniqueboolWhether the COM object gets a managed object of its own rather than the shared one.
contextClassContextThe context in which the code that manages the newly created object will run.
Returns
- T
Type Parameters
TA late-bound wrapper of a dispinterface.
CreateInstance<T>(Guid, ClassContext)
Creates an instance of the COM class identified by clsid and returns it as T.
public static T CreateInstance<T>(Guid clsid, ClassContext context = ClassContext.InprocServer | ClassContext.LocalServer) where T : class
Parameters
clsidGuidThe class identifier.
contextClassContextThe context in which the code that manages the newly created object will run.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
CreateInstance<T>(Guid, bool, ClassContext)
Creates an instance of the COM class identified by clsid and returns it as T,
in a managed object of its own if unique is set.
public static T CreateInstance<T>(Guid clsid, bool unique, ClassContext context = ClassContext.InprocServer | ClassContext.LocalServer) where T : class
Parameters
clsidGuidThe class identifier.
uniquebooltrue for a managed object of its own; false for the object that all the references to the COM object share, which is released when it is garbage collected.
contextClassContextThe context in which the code that manages the newly created object will run.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
Remarks
A unique managed object is not shared with other references to the COM object, so its COM references can be released at once with Release(object?). The objects that its members return are shared, unless they are marshalled as unique objects too.
CreateInstance<T>(string, ClassContext)
Creates an instance of the COM class identified by progId and returns it as T.
public static T CreateInstance<T>(string progId, ClassContext context = ClassContext.InprocServer | ClassContext.LocalServer) where T : class
Parameters
progIdstringThe programmatic identifier of the class, e.g.
Scripting.Dictionary.contextClassContextThe context in which the code that manages the newly created object will run.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
CreateInstance<T>(string, bool, ClassContext)
Creates an instance of the COM class identified by progId and returns it as T,
in a managed object of its own if unique is set, see
CreateInstance<T>(Guid, bool, ClassContext).
public static T CreateInstance<T>(string progId, bool unique, ClassContext context = ClassContext.InprocServer | ClassContext.LocalServer) where T : class
Parameters
progIdstringThe programmatic identifier of the class, e.g.
Scripting.Dictionary.uniqueboolWhether the COM object gets a managed object of its own rather than the shared one.
contextClassContextThe context in which the code that manages the newly created object will run.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
GetActiveInstance<T>(Guid)
Gets the running instance of the COM class identified by clsid, registered in the running object table,
and returns it as T, like Marshal.GetActiveObject of .NET Framework.
public static T GetActiveInstance<T>(Guid clsid) where T : class
Parameters
clsidGuidThe class identifier.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
Exceptions
- COMException
No instance of the class is registered (
MK_E_UNAVAILABLE).
GetActiveInstance<T>(Guid, bool)
Gets the running instance of the COM class identified by clsid, like
GetActiveInstance<T>(Guid), in a managed object of its own if unique is set, see
CreateInstance<T>(Guid, bool, ClassContext).
public static T GetActiveInstance<T>(Guid clsid, bool unique) where T : class
Parameters
clsidGuidThe class identifier.
uniqueboolWhether the COM object gets a managed object of its own rather than the shared one.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
Exceptions
- COMException
No instance of the class is registered (
MK_E_UNAVAILABLE).
GetActiveInstance<T>(string)
Gets the running instance of the COM class identified by progId, registered in the running object
table, and returns it as T, like Marshal.GetActiveObject of .NET Framework.
public static T GetActiveInstance<T>(string progId) where T : class
Parameters
progIdstringThe programmatic identifier of the class, e.g.
Excel.Application.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
Exceptions
- COMException
No instance of the class is registered (
MK_E_UNAVAILABLE).
GetActiveInstance<T>(string, bool)
Gets the running instance of the COM class identified by progId, like
GetActiveInstance<T>(string), in a managed object of its own if unique is set, see
CreateInstance<T>(Guid, bool, ClassContext).
public static T GetActiveInstance<T>(string progId, bool unique) where T : class
Parameters
progIdstringThe programmatic identifier of the class, e.g.
Excel.Application.uniqueboolWhether the COM object gets a managed object of its own rather than the shared one.
Returns
- T
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
Exceptions
- COMException
No instance of the class is registered (
MK_E_UNAVAILABLE).
GetClassId(string)
Gets the class identifier (CLSID) of a programmatic identifier (ProgID).
public static Guid GetClassId(string progId)
Parameters
progIdstringThe programmatic identifier, e.g.
Scripting.Dictionary.
Returns
Exceptions
- COMException
The ProgID is not registered.
TryGetActiveInstance<T>(Guid, bool, out T?)
Gets the running instance of the COM class identified by clsid, like
TryGetActiveInstance<T>(Guid, out T?), in a managed object of its own if unique is
set, see CreateInstance<T>(Guid, bool, ClassContext).
public static bool TryGetActiveInstance<T>(Guid clsid, bool unique, out T? instance) where T : class
Parameters
clsidGuidThe class identifier.
uniqueboolWhether the COM object gets a managed object of its own rather than the shared one.
instanceTThe running instance, or null.
Returns
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
TryGetActiveInstance<T>(Guid, out T?)
Gets the running instance of the COM class identified by clsid, if one is registered in the running
object table, as T.
public static bool TryGetActiveInstance<T>(Guid clsid, out T? instance) where T : class
Parameters
Returns
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
TryGetActiveInstance<T>(string, bool, out T?)
Gets the running instance of the COM class identified by progId, like
TryGetActiveInstance<T>(string, out T?), in a managed object of its own if unique is
set, see CreateInstance<T>(Guid, bool, ClassContext).
public static bool TryGetActiveInstance<T>(string progId, bool unique, out T? instance) where T : class
Parameters
progIdstringThe programmatic identifier of the class, e.g.
Excel.Application.uniqueboolWhether the COM object gets a managed object of its own rather than the shared one.
instanceTThe running instance, or null.
Returns
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.
TryGetActiveInstance<T>(string, out T?)
Gets the running instance of the COM class identified by progId, if one is registered in the running
object table, as T.
public static bool TryGetActiveInstance<T>(string progId, out T? instance) where T : class
Parameters
progIdstringThe programmatic identifier of the class, e.g.
Excel.Application.instanceTThe running instance, or null.
Returns
Type Parameters
TA GeneratedComInterfaceAttribute-attributed interface implemented by the class, or object.