{"id":107206,"date":"2022-09-22T07:00:00","date_gmt":"2022-09-22T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107206"},"modified":"2022-09-22T08:30:15","modified_gmt":"2022-09-22T15:30:15","slug":"20220922-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220922-00\/?p=107206","title":{"rendered":"How can I get WRL to link my object into its activation factory?"},"content":{"rendered":"<p>Recall that under <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20091012-00\/?p=16413\"> the classical model for linking<\/a>, symbols in libraries are not included unless they or some other symbol in the same object file is referenced by an explicitly-included object file, or indirectly via a chain of object files.<\/p>\n<p>Component libraries like ATL and WRL use a linker trick to create self-registering objects. ATL has a set of <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/atl\/reference\/object-map-macros?view=msvc-160\"> object map macros<\/a> for doing this, and WRL has its own macros for <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/cppcx\/wrl\/activatableclass-macros?view=msvc-160\"> activatable objects<\/a> and COM-creatable objects.<\/p>\n<p>But if your object is in a static library, you need to do something to pull in those object files.<\/p>\n<p>For ATL, Larry Osterman used the trick of <a href=\"https:\/\/docs.microsoft.com\/en-us\/archive\/blogs\/larryosterman\/when-i-moved-my-code-into-a-library-what-happened-to-my-atl-com-objects\"> calling two dummy functions in the object file from dead code<\/a>.<\/p>\n<p>WRL codifies the mechanism with a set of macros that create explicit references to the magic objects:<\/p>\n<pre>#define WrlCreatorMapIncludePragma(className)\r\n#define WrlCreatorMapIncludePragmaex(className, serverName)\r\n#define CoCreatableClassWrlCreatorMapInclude(className)\r\n#define CoCreatableClassWrlCreatorMapIncludeEx(className, serverName)\r\n<\/pre>\n<p>Invoke those macros somewhere in the main project (next to your <code>DllMain<\/code> is a fairly obvious place), and they will pull in the object registration information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>More magic macros.<\/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-107206","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>More magic macros.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107206","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=107206"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107206\/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=107206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}