{"id":110165,"date":"2024-08-22T07:00:00","date_gmt":"2024-08-22T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=110165"},"modified":"2024-08-22T08:49:21","modified_gmt":"2024-08-22T15:49:21","slug":"20240822-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240822-00\/?p=110165\/","title":{"rendered":"The role of the activation factory in the Windows Runtime"},"content":{"rendered":"<p>The Windows Runtime has these things called activation factories, which you obtain by calling <code>Ro\u00adGet\u00adActivation\u00adFactory<\/code>. What is an activation factory?<\/p>\n<p>The primary purpose of an activation factory is given in its name: To activate (create) objects. Every activation factory implements the <code>IActivation\u00adFactory<\/code> interface, which has a single method: <code>Activate\u00adInstance<\/code>. This method creates an object and returns it.<\/p>\n<p>Now, the <code>IActivation\u00adFactory::<wbr \/>Activate\u00adInstance<\/code> 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&#8217;ll need more.<\/p>\n<p>Non-default constructors for a class are placed on a custom interface conventionally named <code>IWidget\u00adFactory<\/code>. For example, if you had a runtime class which had a constructor that took a string parameter:<\/p>\n<pre>runtimeclass Widget\r\n{\r\n    Widget(String name);\r\n}\r\n<\/pre>\n<p>then the <code>IWidget\u00adFactory<\/code> interface would have a method like<\/p>\n<pre>HRESULT IWidgetFactory::CreateInstance([in] HSTRING name, [out, retval] IWidget** result);\r\n<\/pre>\n<p>The parameters to the constructor are the parameters to the <code>Create\u00adInstance<\/code> method, and the output of the <code>Create\u00adInstance<\/code> method is the newly-created object.<\/p>\n<p>The other thing that is provided by the activation factory is the class&#8217;s static members. The static members are on an interface conventionally named <code>IWidget\u00adStatics<\/code>. For example, if we had a static method <code>FindByName<\/code>:<\/p>\n<pre>runtimeclass Widget\r\n{\r\n    static Widget FindByName(String name);\r\n}\r\n<\/pre>\n<p>Then the <code>IWidget\u00adStatics<\/code> interface would have a method like<\/p>\n<pre>HRESULT IWidgetStatics::FindByName([in] HSTRING name, [out, retval] IWidget** result);\r\n<\/pre>\n<p>In summary, the activation factory is a place to put all the things that a class can do which aren&#8217;t instance members. It&#8217;s the object that represents the class itself, rather than any instances of it.<\/p>\n<p><b>Bonus chatter<\/b>: If you think of a constructor as a &#8220;static method called <code>Create\u00adInstance<\/code> that returns a newly-constructed object&#8221;, then you can think of the activation factory as the place to put all the static members.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The activation factory represents the class itself.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-110165","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The activation factory represents the class itself.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=110165"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110165\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=110165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=110165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=110165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}