Export (0) Print
Expand All

ExtensibleClassFactory Class

Enables customization of managed objects that extend from unmanaged objects during creation.

For a list of all members of this type, see ExtensibleClassFactory Members.

System.Object
   System.Runtime.InteropServices.ExtensibleClassFactory

[Visual Basic]
NotInheritable Public Class ExtensibleClassFactory
[C#]
public sealed class ExtensibleClassFactory
[C++]
public __gc __sealed class ExtensibleClassFactory
[JScript]
public class ExtensibleClassFactory

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The ExtensibleClassFactory allows users to specify a delegate that is called during construction of a runtime callable wrapper (RCW) that provides an instance of the underlying COM object. In effect, the callback acts as the class factory for the COM object wrapped by the RCW. Without the callback, the common language runtime creates the underlying COM object by calling CoCreateInstance. This callback provides an alternative way of activating the underlying object, such as with a COM moniker or by providing a singleton object. The RegisterObjectCreationCallback method must be called in the static (Shared in Visual Basic) initializer of the class that is extending the RCW. Only one object creation callback is permitted per object type. When the extensible RCW is activated, the callback is registered. When the underlying COM object needs to be created, the callback is called to provide a reference to the object. The callback must return an IUnknown interface pointer for the base object.

Example

[Visual Basic, C#, C++] Registers a delegate that will be called whenever an instance of a managed type that extends from an unmanaged type needs to allocate the aggregated unmanaged object. This delegate is expected to allocate and aggregate the unmanaged object and is called in place of a CoCreateInstance. This routine must be called in the context of the static (Shared in Visual Basic) initializer for the class for which the callbacks will be made.

[Visual Basic] 
Imports System
Imports System.Runtime.InteropServices

Public Class CallBack
    
    Public Function Activate(Aggregator As IntPtr) As IntPtr
        Dim oCOM As New ECFSRV32Lib.ObjectActivator()
        Dim itf As ECFSRV32Lib.IObjectActivator = _
           CType(oCOM, ECFSRV32Lib.IObjectActivator)
        Return New IntPtr(itf.CreateBaseComponent(Aggregator.ToInt32()))
    End Function
End Class

'
' The EcfInner class. First .NET class derived directly from COM class.
'
Public Class EcfInner
    Inherits ECFSRV32Lib.BaseComponent
    Private Shared callbackInner As CallBack    
    
    Shared Sub RegisterInner()
        callbackInner = New CallBack()
        ExtensibleClassFactory.RegisterObjectCreationCallback( _
           New System.Runtime.InteropServices.ObjectCreationDelegate( _
           AddressOf callbackInner.Activate))
    End Sub    
    
    'This is the static initializer.    
    Shared Sub New()
        RegisterInner()
    End Sub
End Class

[C#] 
using System;
using System.Runtime.InteropServices;

public class CallBack
{
   public IntPtr Activate(IntPtr Aggregator)
   {
      ECFSRV32Lib.ObjectActivator oCOM = new ECFSRV32Lib.ObjectActivator();
      ECFSRV32Lib.IObjectActivator itf = (ECFSRV32Lib.IObjectActivator)oCOM;
      return (IntPtr) itf.CreateBaseComponent((int)Aggregator);
   }
}

//
// The EcfInner class. First .NET class derived directly from COM class.
//
public class EcfInner : ECFSRV32Lib.BaseComponent
{
   static CallBack callbackInner;

   static void RegisterInner()
   {      
      callbackInner = new CallBack();
      System.Runtime.InteropServices.ExtensibleClassFactory.RegisterObjectCreationCallback(new System.Runtime.InteropServices.ObjectCreationDelegate(callbackInner.Activate));
   }

   //This is the static initializer.    
   static EcfInner()
   {
      RegisterInner();
   }
}

[C++] 
public __gc class CallBack
{
public:
   IntPtr Activate(IntPtr Aggregator)
   {
      ECFSRV32Lib::ObjectActivator* oCOM = new ECFSRV32Lib::ObjectActivator();
      ECFSRV32Lib::IObjectActivator* itf = dynamic_cast<ECFSRV32Lib::IObjectActivator*>(oCOM);
      return (IntPtr) itf->CreateBaseComponent((int)Aggregator);
   }
};

//
// The EcfInner class. First .NET class derived directly from COM class.
//
public __gc class EcfInner : public ECFSRV32Lib::BaseComponent
{
   static CallBack* callbackInner;

   static void RegisterInner()
   {      
      callbackInner = new CallBack();
      System::Runtime::InteropServices::ExtensibleClassFactory::RegisterObjectCreationCallback(
         new System::Runtime::InteropServices::ObjectCreationDelegate(callbackInner, &CallBack::Activate));
   }

   //This is the static initializer.    
   static EcfInner()
   {
      RegisterInner();
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

ExtensibleClassFactory Members | System.Runtime.InteropServices Namespace

Show:
© 2014 Microsoft