The Windows Runtime has these things called activation factories, which you obtain by calling RoÂGetÂActivationÂFactory
. What is an activation factory?
The primary purpose of an activation factory is given in its name: To activate (create) objects. Every activation factory implements the IActivationÂFactory
interface, which has a single method: ActivateÂInstance
. This method creates an object and returns it.
Now, the IActivationÂFactory::
method does not take any input parameters, so this can be used only if your object has a default constructor (no parameters). If your class has constructors which take parameters, then you’ll need more.
Non-default constructors for a class are placed on a custom interface conventionally named IWidgetÂFactory
. For example, if you had a runtime class which had a constructor that took a string parameter:
runtimeclass Widget { Widget(String name); }
then the IWidgetÂFactory
interface would have a method like
HRESULT IWidgetFactory::CreateInstance([in] HSTRING name, [out, retval] IWidget** result);
The parameters to the constructor are the parameters to the CreateÂInstance
method, and the output of the CreateÂInstance
method is the newly-created object.
The other thing that is provided by the activation factory is the class’s static members. The static members are on an interface conventionally named IWidgetÂStatics
. For example, if we had a static method FindByName
:
runtimeclass Widget { static Widget FindByName(String name); }
Then the IWidgetÂStatics
interface would have a method like
HRESULT IWidgetStatics::FindByName([in] HSTRING name, [out, retval] IWidget** result);
In summary, the activation factory is a place to put all the things that a class can do which aren’t instance members. It’s the object that represents the class itself, rather than any instances of it.
Bonus chatter: If you think of a constructor as a “static method called CreateÂInstance
that returns a newly-constructed object”, then you can think of the activation factory as the place to put all the static members.
Ad naming, I would have thought that IActivationFactory would be a factory for activations.
So RoGetActivationFactory is to IActivationFactory as CoGetClassObject is to IClassFactory?
I think the big thing to remember is that, just like the object returned through CoGetClassObject doesn't necessarily have to allow the creation of an instance through IClassFactory (there is the predefined IClassFactory2, and custom factory interfaces can be defined), the object returned through RoGetActivationFactory doesn't have to allow the creation of an instance through IActivationFactory.
But in general, both of these functions are used to retrieve the so called factory object for the coclass/runtimeclass.