August 22nd, 2024

The role of the activation factory in the Windows Runtime

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::Activate­Instance 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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

3 comments

Discussion is closed. Login to edit/delete existing comments.

Newest
Newest
Popular
Oldest
  • IS4

    Ad naming, I would have thought that IActivationFactory would be a factory for activations.

  • Kyle Sluder

    So RoGetActivationFactory is to IActivationFactory as CoGetClassObject is to IClassFactory?

    • Me Gusta

      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.

      Read more

Feedback