{"id":10453,"date":"2011-06-10T07:00:00","date_gmt":"2011-06-10T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/06\/10\/generally-speaking-if-your-function-fails-you-should-return-a-failure-code\/"},"modified":"2011-06-10T07:00:00","modified_gmt":"2011-06-10T07:00:00","slug":"generally-speaking-if-your-function-fails-you-should-return-a-failure-code","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110610-00\/?p=10453","title":{"rendered":"Generally speaking, if your function fails, you should return a failure code"},"content":{"rendered":"<p>\nA customer\nrequested assistance with their shell namespace extension,\nand the request worked its way to me for resolution.\n<\/p>\n<pre>\nUnhandled exception at 0x76fab89c (shell32.dll) in explorer.exe: 0xC0000005:\nAccess violation reading location 0x00000000.\nshell32.dll!CShellItem::_GetPropertyStoreWorker()  + 0x44 bytes\nshell32.dll!CShellItem::GetPropertyStoreForKeys()  + 0x38 bytes\nthumbcache.dll!CThumbnailCache::_GetMonikerDataFromShellItem()  + 0x8b bytes\nthumbcache.dll!CThumbnailCache::GetThumbnail()  + 0x11c bytes\nshell32.dll!CSetOperationCallback::_LookupThumbnail()  + 0x8d bytes\nshell32.dll!CSetOperationCallback::_PrefetchCachedThumbnails()  + 0xb6 bytes\nshell32.dll!CSetOperationCallback::OnNextBatch()  + 0x4f bytes\nshell32.dll!CEnumTask::_PushBatchToView()  + 0x68 bytes\nshell32.dll!CEnumTask::_IncrFillEnumToView()  + 0x2ca5 bytes\nshell32.dll!CEnumTask::_IncrEnumFolder()  + 0x8da5a bytes\nshell32.dll!CEnumTask::InternalResumeRT()  + 0xa6 bytes\nshell32.dll!CRunnableTask::Run()  + 0x92 bytes\nbrowseui.dll!CShellTask::TT_Run()  + 0x2d bytes\nbrowseui.dll!CShellTaskThread::ThreadProc()  + 0x87 bytes\nbrowseui.dll!CShellTaskThread::s_ThreadProc()  + 0x21 bytes\nshlwapi.dll!_ExecuteWorkItemThreadProc@4()  + 0xe bytes\nntdll.dll!_RtlpTpWorkCallback@8()  + 0xaa bytes\nntdll.dll!_TppWorkerThread@4()  + 0x274 bytes\nkernel32.dll!@BaseThreadInitThunk@12()  + 0x12 bytes\nntdll.dll!__RtlUserThreadStart@8()  + 0x27 bytes\n<\/pre>\n<p>\nThe customer was at a loss because the customer&#8217;s code was\nnowhere on the stack.\nWhat is wrong?\n<\/p>\n<p>\nThe customer didn&#8217;t provide a dump file or any other information\nbeyond the stack trace.\n(Hint: When reporting a problem with a shell namespace extension,\nat least mention the last few method calls your namespace extension\nreceived before the crash.)\nI was forced to use my psychic powers to solve the problem.\nBut you can, too.\nAll the information you need is right there in front of you.\n<\/p>\n<p>\nThe shell faulted on a null pointer in the function\n<code>CShellItem::_GetPropertyStoreWorker<\/code>,\nwhich from its name is clearly a worker function which\nobtains the property store from a shell item.\n<\/p>\n<p>\nAt this point, you put on your thinking cap.\nWhy is the shell taking a null pointer fault trying to retrieve\nthe property store from a shell item?\nRemember that the problem is tied to a custom namespace extension.\n<\/p>\n<blockquote CLASS=\"m\"><p>\nMy psychic powers tell me that the namespace extension\nreturned <code>S_OK<\/code>\nfrom <code>GetUIObjectOf(IPropertyStoreFactory)<\/code>\nbut set the output pointer to <code>NULL<\/code>.\n<\/p><\/blockquote>\n<p>\n(It turns out my psychic powers were weak without coffee, because\nthe initial psychic diagnosis was <code>GetUIObjecttOf(IPropertyStore)<\/code>\ninstead of <code>IPropertyStoreFactory<\/code>.)\n<\/p>\n<p>\nAs a general rule, if your function fails, then you should\nreturn a failure code, not a success code.\nThere are exceptions to this rule, particular when OLE automation\nis involved, but it&#8217;s a good rule to start with.\n<\/p>\n<p>\nThe customer reported that fixing their\n<code>IShellFolder::BindToObject<\/code> to return an error code\nwhen it failed fixed the problem.\nThe customer then followed up with another crash, again providing\nstartling little information.\n<\/p>\n<pre>\nUnhandled exception at 0x763cf7e7 (shell32.dll) in explorer.exe: 0xC0000005:\nAccess violation reading location 0x000a0d70.\nCall Stack:\nshell32.dll!CInfotipTask::InternalResumeRT() + 0x2e bytes\nshell32.dll!CRunnableTask::Run() + 0x92 bytes\nbrowseui.dll!CShellTask::TT_Run() + 0x2d bytes\nbrowseui.dll!CShellTaskThread::ThreadProc() + 0x87 bytes\nbrowseui.dll!CShellTaskThread::s_ThreadProc() + 0x21 bytes\nshlwapi.dll!_ExecuteWorkItemThreadProc@4() + 0xe bytes\nntdll.dll!_RtlpTpWorkCallback@8() + 0xaa bytes\nntdll.dll!_TppWorkerThread@4() + 0x274 bytes\nkernel32.dll!@BaseThreadInitThunk@12() + 0x12 bytes\nntdll.dll!__RtlUserThreadStart@8() + 0x27 bytes\n<\/pre>\n<p>\nThe customer reported that\n<code>IQueryInfo::SetInfoTip<\/code> is getting called.\nThe customer liaison added,\n&#8220;Raymond, I&#8217;m looking forward to your psychic powers again.&#8221;\n<\/p>\n<p>\nApparently, some people don&#8217;t understand that psychic powers are not\nsomething you ask for.\nIt&#8217;s my way of scolding you for not providing enough information\nto make a quality diagnosis possible.\nYou don&#8217;t come back saying,\n&#8220;Hey, thanks for answering my question even though I did a crappy job\nof asking it.\nHere&#8217;s another crappy question!&#8221;\n<\/p>\n<p>\nI reported back that my psychic powers were growing weary from overuse,\nand that the customer might want to expend a little more time investigating\nthe problem themselves.\nEspecially since it has the same root cause as their previous problem.\n<\/p>\n<p>\n[Raymond is currently away; this message was pre-recorded.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer requested assistance with their shell namespace extension, and the request worked its way to me for resolution. Unhandled exception at 0x76fab89c (shell32.dll) in explorer.exe: 0xC0000005: Access violation reading location 0x00000000. shell32.dll!CShellItem::_GetPropertyStoreWorker() + 0x44 bytes shell32.dll!CShellItem::GetPropertyStoreForKeys() + 0x38 bytes thumbcache.dll!CThumbnailCache::_GetMonikerDataFromShellItem() + 0x8b bytes thumbcache.dll!CThumbnailCache::GetThumbnail() + 0x11c bytes shell32.dll!CSetOperationCallback::_LookupThumbnail() + 0x8d bytes shell32.dll!CSetOperationCallback::_PrefetchCachedThumbnails() + 0xb6 [&hellip;]<\/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-10453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer requested assistance with their shell namespace extension, and the request worked its way to me for resolution. Unhandled exception at 0x76fab89c (shell32.dll) in explorer.exe: 0xC0000005: Access violation reading location 0x00000000. shell32.dll!CShellItem::_GetPropertyStoreWorker() + 0x44 bytes shell32.dll!CShellItem::GetPropertyStoreForKeys() + 0x38 bytes thumbcache.dll!CThumbnailCache::_GetMonikerDataFromShellItem() + 0x8b bytes thumbcache.dll!CThumbnailCache::GetThumbnail() + 0x11c bytes shell32.dll!CSetOperationCallback::_LookupThumbnail() + 0x8d bytes shell32.dll!CSetOperationCallback::_PrefetchCachedThumbnails() + 0xb6 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10453","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=10453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10453\/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=10453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=10453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=10453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}