{"id":109178,"date":"2023-12-22T07:00:00","date_gmt":"2023-12-22T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109178"},"modified":"2023-12-22T07:27:49","modified_gmt":"2023-12-22T15:27:49","slug":"20231222-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20231222-00\/?p=109178","title":{"rendered":"If I don&#8217;t have any items, what error should my <CODE>IFolderView::Items<\/CODE> method return?"},"content":{"rendered":"<p>A customer wanted to know what error code their shell extension should return from the <code>IFolderView::<wbr \/>Items<\/code> method if their folder has no items.<\/p>\n<p>This was a trick question, because you shouldn&#8217;t return an error at all!<\/p>\n<p>Returning an error means, &#8220;I was unable to retrieve the list of items. Something terrible happened!&#8221; This generally leads to a cascade of failures, and Explorer gives up on your shell extension.<\/p>\n<p>If you want to say &#8220;I have no items,&#8221; then return <code>S_OK<\/code> and an empty collection. If the requested interface is <code>IEnumIDList<\/code>, then return an enumerator that enumerates on objects. If the requested interface is <code>IShellItemArray<\/code>, then return a zero-sized array.<\/p>\n<p>The point is that you want to say &#8220;I was able to get the items. And here they are!&#8221; (And you give them an empty collection.)<\/p>\n<p>This is a general principle for all collection-returning functions: If you want to return an empty collection, then return an empty collection.\u00b9 Don&#8217;t return an error.<\/p>\n<p>Embrace the power of the empty set.<\/p>\n<p>By analogy, suppose you had a method<\/p>\n<pre>class WidgetContainer\r\n{\r\npublic:\r\n    std::vector&lt;Widget&gt; GetActiveWidgets();\r\n};\r\n<\/pre>\n<p>If there are no active Widgets in the WidgetContainer, you shouldn&#8217;t throw an exception. You should just return an empty <code>std::vector<\/code>.<\/p>\n<p>\u00b9 In this case, the customer also tried returning <code>S_OK<\/code> and a null pointer from the <code>IFolderView::<wbr \/>Items<\/code> method. This caused Explorer to crash on a null pointer, because Explorer asked for the items, and you said, &#8220;Sure, here they are!&#8221; and gave a null pointer.<\/p>\n<p>There are some methods, such as <code>IShellFolder::<wbr \/>EnumObjects<\/code>, which carve out a special case that permits you to return <code>S_FALSE<\/code> and a null pointer as the result. But these are special cases that apply only if the method explicitly permits it. And even then, they don&#8217;t allow you return a failure; returning a failure means that you were unable to get the items at all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It shouldn&#8217;t return an error at all.<\/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-109178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It shouldn&#8217;t return an error at all.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109178","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=109178"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109178\/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=109178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}