{"id":92341,"date":"2015-12-07T07:00:00","date_gmt":"2015-12-07T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20151207-00\/?p=92341\/"},"modified":"2019-03-13T12:22:34","modified_gmt":"2019-03-13T19:22:34","slug":"20151207-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20151207-00\/?p=92341","title":{"rendered":"Enumerating all the programs that can launch a particular protocol"},"content":{"rendered":"<p>Today&#8217;s Little Program is a variation on the one from <!-- backref: Enumerating all the programs that can open a particular file extension -->last time<\/a>. This time, instead of enumerating all the handlers for a file extension, we enumerate all the handlers for a protocol. We then open a Web site with that chosen program. <\/p>\n<p>Start with the program from last time and make these changes: <\/p>\n<pre>\nstd::vector&lt;CComPtr&lt;IAssocHandler&gt;&gt; LoadHandlers(\n  PCWSTR extension)\n  \/\/ <font COLOR=\"red\"><strike>ASSOC_FILTER filter<\/strike><\/font>\n  )\n{\n  std::vector&lt;CComPtr&lt;IAssocHandler&gt;&gt; handlers;\n  CComPtr&lt;IEnumAssocHandlers&gt; enumerator;\n  <font COLOR=\"blue\">SHAssocEnumHandlersForProtocolByApplication(\n    L\"http\", IID_PPV_ARGS(&amp;enumerator));<\/font>\n  for (CComPtr&lt;IAssocHandler&gt; handler;\n       enumerator-&gt;Next(1, &amp;handler, nullptr) == S_OK;\n       handler.Release()) {\n       handlers.push_back(handler);\n  }\n  return handlers;\n}\n<\/pre>\n<p>The <code>SHAssoc&shy;Enum&shy;Handlers&shy;ForProtocol&shy;By&shy;Application<\/code> function does not have a filter option, so we delete that parameter from <code>Load&shy;Handlers<\/code>. <\/p>\n<p>The <code>Choose&shy;Handler<\/code> function is unchanged. <\/p>\n<pre>\nint __cdecl main(int, char**)\n{\n  CCoInitialize init;\n  ProcessReference ref;\n\n  <font COLOR=\"blue\">auto handlers = LoadHandlers(L\"http\");\n  auto selection = ChooseHandler(handlers, false);<\/font>\n\n  if (selection &lt; handlers.size()) {\n    CComPtr&lt;IDataObject&gt; dobj;\n    GetUIObjectOfFile(nullptr, <font COLOR=\"blue\">L\"http:\/\/www.microsoft.com\/\"<\/font>,\n                      IID_PPV_ARGS(&amp;dobj));\n    handlers[selection]-&gt;Invoke(dobj);\n  }\n  return 0;\n}\n<\/pre>\n<p>This version is shorter because there is no filter option, so we just load up all the handlers, pick one, and invoke it. <\/p>\n<p>Notice that we are using <code>Get&shy;UI&shy;Object&shy;Of&shy;File<\/code> for something that isn&#8217;t a file. If you go back to that function, you&#8217;ll see that there&#8217;s nothing in it that actually requires a file. It can accept any parseable name. A more accurate name for the function would have been <code>Get&shy;UI&shy;Object&shy;Of&shy;Parsing&shy;Name<\/code>, but it&#8217;s too late now. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>SHAssocEnumHandlersForProtocolByApplication<\/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-92341","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>SHAssocEnumHandlersForProtocolByApplication<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/92341","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=92341"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/92341\/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=92341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=92341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=92341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}