{"id":104801,"date":"2021-02-05T07:00:00","date_gmt":"2021-02-05T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104801"},"modified":"2021-05-05T13:28:04","modified_gmt":"2021-05-05T20:28:04","slug":"2021025-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210205-00\/?p=104801","title":{"rendered":"How do I get a foothold in the neutral apartment?"},"content":{"rendered":"<p>The neutral apartment is a mysterious beast. You cannot initialize a thread in the neutral apartment, because the neutral apartment isn&#8217;t tied to a thread. Instead, the neutral apartment <i>takes over<\/i> any thread it can find: If you are on an existing thread and call an object that lives in the neutral apartment, the calling thread is <i>converted<\/i> to a neutral apartment thread temporarily. When the call is finished, the thread is returned to its original apartment.<\/p>\n<p>That&#8217;s great, but now there&#8217;s a chicken-and-egg problem: How do you get the first object into the neutral apartment?<\/p>\n<p>One way is to <code>CoCreateInstance<\/code> an object whose definition specifies that it runs in the neutral apartment. But maybe you don&#8217;t have one of those objects readily at hand. What if you just want to get into the neutral apartment directly?<\/p>\n<p>The <code>Co\u00adGet\u00adDefault\u00adContext<\/code> function will produce an <code>IContext\u00adCallback<\/code> for the apartment type you specify.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th><tt>APTTYPE<\/tt><\/th>\n<th>Result<\/th>\n<\/tr>\n<tr>\n<td>CURRENT<\/td>\n<td>Same as <code>Co\u00adGet\u00adObject\u00adContext<\/code><\/td>\n<\/tr>\n<tr>\n<td>STA<\/td>\n<td>Not allowed<\/td>\n<\/tr>\n<tr>\n<td>MTA<\/td>\n<td>Returns the multithreaded apartment<\/td>\n<\/tr>\n<tr>\n<td>NA<\/td>\n<td>Returns the neutral apartment<\/td>\n<\/tr>\n<tr>\n<td>MAINSTA<\/td>\n<td>Returns the main STA<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Passing <code>APTTYPE_<wbr \/>STA<\/code> is not valid because it&#8217;s ambiguous: There can be multiple STA apartments in a process.<\/p>\n<p>Asking for the <code>APTTYPE_<wbr \/>MAINSTA<\/code> when there is no main STA will fail. And asking for anything when COM isn&#8217;t initialized will also fail.<\/p>\n<p>And so we find our foothold: Calling <code>Co\u00adGet\u00adDefault\u00adContext<\/code> with <code>APTTYPE_<wbr \/>NA<\/code> will return the context for the neutral apartment. You can then <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191128-00\/?p=103157\"> use the <code>IContext\u00adCallback::<wbr \/>Context\u00adCallback<\/code> method<\/a> to execute code in that context.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can explicit ask for its context.<\/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-104801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can explicit ask for its context.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104801","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=104801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104801\/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=104801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}