{"id":103000,"date":"2019-10-16T07:00:00","date_gmt":"2019-10-16T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103000"},"modified":"2019-10-15T19:58:33","modified_gmt":"2019-10-16T02:58:33","slug":"20191016-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191016-00\/?p=103000","title":{"rendered":"Why do I get a <CODE>winrt::hresult_no_interface<\/CODE> exception when I try to register an auto-revoke event handler?"},"content":{"rendered":"<p>If you register an event handler in C++\/WinRT, the registration function returns a <code>winrt::<\/code><code>event_<\/code><code>token<\/code>, and you can pass that event token back to the event in order to unregister the handler.<\/p>\n<p>An alternative that is popular in some circles is to register with <code>winrt::auto_revoke<\/code> as a marker first parameter ahead of the usual event handler parameters. If you do this, then the registration function returns a <code>winrt::event_revoker<\/code> instead of a <code>winrt::<\/code><code>event_<\/code><code>token<\/code>. The <code>event_<\/code><code>revoker<\/code> automatically unregisters the event at destruction.<\/p>\n<p>What does it mean when attempting to register an auto-revoke event handler throws the <code>hresult_<\/code><code>no_<\/code><code>interface<\/code> exception?<\/p>\n<p>Internally, the <code>event_<\/code><code>revoker<\/code> is a class with three members:<\/p>\n<ul>\n<li>A weak reference to the event source.<\/li>\n<li>A method to call to unregister the event.<\/li>\n<li>The <code>event_<\/code><code>token<\/code> to unregister with.<\/li>\n<\/ul>\n<p>The <code>hresult_<\/code><code>no_<\/code><code>interface<\/code> exception can come out of the first part: Creating the weak reference. You get the exception if the event source doesn&#8217;t support weak references. (This is common in the <code>Windows.<\/code><code>UI.<\/code><code>Composition<\/code> namespace.) In that case, you cannot use the auto-revoke feature will have to fall back to revoking your event handlers manually.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The necessary infrastructure isn&#8217;t there.<\/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-103000","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The necessary infrastructure isn&#8217;t there.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103000","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=103000"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103000\/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=103000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}