{"id":12253,"date":"2010-11-18T07:00:00","date_gmt":"2010-11-18T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/11\/18\/one-possible-reason-why-shellexecute-returns-se_err_accessdenied-and-shellexecuteex-returns-error_access_denied\/"},"modified":"2010-11-18T07:00:00","modified_gmt":"2010-11-18T07:00:00","slug":"one-possible-reason-why-shellexecute-returns-se_err_accessdenied-and-shellexecuteex-returns-error_access_denied","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20101118-00\/?p=12253","title":{"rendered":"One possible reason why ShellExecute returns SE_ERR_ACCESSDENIED and ShellExecuteEx returns ERROR_ACCESS_DENIED"},"content":{"rendered":"<p>(The strangely-phrased subject line is for search engine optimization.)\n A customer reported that when they called <code>ShellExecute<\/code>, the function sometimes fails with <code>SE_ERR_ACCESSDENIED<\/code>, depending on what they are trying to execute. (If they had tried <code>ShellExecuteEx<\/code> <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/11\/26\/270710.aspx#270767\"> they would have gotten the error <code>ERROR_ACCESS_DENIED<\/code><\/a>.)\n After a good amount of back-and-forth examing file type registrations, a member of the development team had psychic insight to ask, &#8220;Are you calling it from an MTA?&#8221;\n &#8220;Yes,&#8221; the customer replied. &#8220;ShellExecute is being called from a dedicated MTA thread. Would that cause the failure?&#8221;\n Why yes, as a matter of fact, and it&#8217;s called out in the documentation for <code>ShellExecute<\/code>.<\/p>\n<blockquote class=\"m\"><p> Because <b>ShellExecute<\/b> can delegate execution to Shell extensions (data sources, context menu handlers, verb implementations) that are activated using Component Object Model (COM), COM should be initialized before <b>ShellExecute<\/b> is called. Some Shell extensions require the COM single-threaded apartment (STA) type. <\/p><\/blockquote>\n<p> As a general rule, <a href=\"http:\/\/support.microsoft.com\/kb\/287087\"> shell functions require STA<\/a>. Recall that <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/04\/24\/8420242.aspx\"> MTA implies no user interface<\/a>. If you try to use an apartment-threaded object from your MTA thread, a marshaller is required, and <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/12\/13\/281910.aspx\"> if no such marshaller exists, the call fails<\/a>.<\/p>\n<p> This also explains why the failure occurs only for certain file types: If handling the file type happens not to involve creating a COM object, then the MTA\/STA mismatch situation never occurs. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>(The strangely-phrased subject line is for search engine optimization.) A customer reported that when they called ShellExecute, the function sometimes fails with SE_ERR_ACCESSDENIED, depending on what they are trying to execute. (If they had tried ShellExecuteEx they would have gotten the error ERROR_ACCESS_DENIED.) After a good amount of back-and-forth examing file type registrations, a member [&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-12253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>(The strangely-phrased subject line is for search engine optimization.) A customer reported that when they called ShellExecute, the function sometimes fails with SE_ERR_ACCESSDENIED, depending on what they are trying to execute. (If they had tried ShellExecuteEx they would have gotten the error ERROR_ACCESS_DENIED.) After a good amount of back-and-forth examing file type registrations, a member [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12253","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=12253"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12253\/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=12253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=12253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=12253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}