/// <summary> /// Create a COM representation of the supplied object that can be passed to a non-managed environment. /// </summary> /// <param name="impl">The <see cref="ComWrappers" /> implementation to use when creating the COM representation.</param> /// <param name="instance">The managed object to expose outside the .NET runtime.</param> /// <param name="flags">Flags used to configure the generated interface.</param> /// <param name="retValue">The generated COM interface that can be passed outside the .NET runtime or IntPtr.Zero if it could not be created.</param> /// <returns>Returns <c>true</c> if a COM representation could be created, <c>false</c> otherwise</returns> /// <remarks> /// If <paramref name="impl" /> is <c>null</c>, the global instance (if registered) will be used. /// </remarks> private static bool TryGetOrCreateComInterfaceForObjectInternal( ComWrappers impl, object instance, CreateComInterfaceFlags flags, out IntPtr retValue ) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } return(TryGetOrCreateComInterfaceForObjectInternal( ObjectHandleOnStack.Create(ref impl), impl.id, ObjectHandleOnStack.Create(ref instance), flags, out retValue )); }
/// <summary> /// Get the currently registered managed object or creates a new managed object and registers it. /// </summary> /// <param name="impl">The <see cref="ComWrappers" /> implementation to use when creating the managed object.</param> /// <param name="externalComObject">Object to import for usage into the .NET runtime.</param> /// <param name="innerMaybe">The inner instance if aggregation is involved</param> /// <param name="flags">Flags used to describe the external object.</param> /// <param name="wrapperMaybe">The <see cref="object"/> to be used as the wrapper for the external object.</param> /// <param name="retValue">The managed object associated with the supplied external COM object or <c>null</c> if it could not be created.</param> /// <returns>Returns <c>true</c> if a managed object could be retrieved/created, <c>false</c> otherwise</returns> /// <remarks> /// If <paramref name="impl" /> is <c>null</c>, the global instance (if registered) will be used. /// </remarks> private static bool TryGetOrCreateObjectForComInstanceInternal( ComWrappers impl, IntPtr externalComObject, IntPtr innerMaybe, CreateObjectFlags flags, object?wrapperMaybe, out object?retValue) { ArgumentNullException.ThrowIfNull(externalComObject); // If the inner is supplied the Aggregation flag should be set. if (innerMaybe != IntPtr.Zero && !flags.HasFlag(CreateObjectFlags.Aggregation)) { throw new InvalidOperationException(SR.InvalidOperation_SuppliedInnerMustBeMarkedAggregation); } object?wrapperMaybeLocal = wrapperMaybe; retValue = null; return(TryGetOrCreateObjectForComInstanceInternal(ObjectHandleOnStack.Create(ref impl), impl.id, externalComObject, innerMaybe, flags, ObjectHandleOnStack.Create(ref wrapperMaybeLocal), ObjectHandleOnStack.Create(ref retValue))); }
/// <summary> /// Register a <see cref="ComWrappers" /> instance to be used as the global instance for marshalling in the runtime. /// </summary> /// <param name="instance">Instance to register</param> /// <remarks> /// This function can only be called a single time. Subsequent calls to this function will result /// in a <see cref="System.InvalidOperationException"/> being thrown. /// /// Scenarios where this global instance may be used are: /// * Usage of COM-related Marshal APIs /// * P/Invokes with COM-related types /// * COM activation /// </remarks> public static void RegisterForMarshalling(ComWrappers instance) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if ( null != Interlocked.CompareExchange(ref s_globalInstanceForMarshalling, instance, null) ) { throw new InvalidOperationException( SR.InvalidOperation_ResetGlobalComWrappersInstance ); } // Indicate to the runtime that a global instance has been registered for marshalling. // This allows the native runtime know to call into the managed ComWrappers only if a // global instance is registered for marshalling. SetGlobalInstanceRegisteredForMarshalling(instance.id); }
/// <summary> /// Register a <see cref="ComWrappers" /> instance to be used as the global instance for reference tracker support. /// </summary> /// <param name="instance">Instance to register</param> /// <remarks> /// This function can only be called a single time. Subsequent calls to this function will result /// in a <see cref="System.InvalidOperationException"/> being thrown. /// /// Scenarios where this global instance may be used are: /// * Object tracking via the <see cref="CreateComInterfaceFlags.TrackerSupport" /> and <see cref="CreateObjectFlags.TrackerObject" /> flags. /// </remarks> public static void RegisterForTrackerSupport(ComWrappers instance) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if ( null != Interlocked.CompareExchange( ref s_globalInstanceForTrackerSupport, instance, null ) ) { throw new InvalidOperationException( SR.InvalidOperation_ResetGlobalComWrappersInstance ); } SetGlobalInstanceRegisteredForTrackerSupport(instance.id); }
public static object GetObjectForIUnknown(IntPtr pUnk) { return(ComWrappers.ComObjectForInterface(pUnk)); }
public static IntPtr GetIUnknownForObject(object o) { return(ComWrappers.ComInterfaceForObject(o)); }
public static void RegisterForMarshalling(ComWrappers instance) { throw new PlatformNotSupportedException(); }
public static void RegisterForTrackerSupport(ComWrappers instance) { throw new PlatformNotSupportedException(); }
/// <summary> /// Create a COM representation of the supplied object that can be passed to a non-managed environment. /// </summary> /// <param name="impl">The <see cref="ComWrappers" /> implementation to use when creating the COM representation.</param> /// <param name="instance">The managed object to expose outside the .NET runtime.</param> /// <param name="flags">Flags used to configure the generated interface.</param> /// <param name="retValue">The generated COM interface that can be passed outside the .NET runtime or IntPtr.Zero if it could not be created.</param> /// <returns>Returns <c>true</c> if a COM representation could be created, <c>false</c> otherwise</returns> /// <remarks> /// If <paramref name="impl" /> is <c>null</c>, the global instance (if registered) will be used. /// </remarks> private static bool TryGetOrCreateComInterfaceForObjectInternal(ComWrappers impl, object instance !!, CreateComInterfaceFlags flags, out IntPtr retValue)