{"id":401,"date":"2010-04-05T15:34:26","date_gmt":"2010-04-05T14:34:26","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=401"},"modified":"2010-04-05T15:37:11","modified_gmt":"2010-04-05T14:37:11","slug":"how-to-handle-custom-url-protocols-with-the-microsoft-webbrowser-control","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2010\/04\/05\/how-to-handle-custom-url-protocols-with-the-microsoft-webbrowser-control\/","title":{"rendered":"How To Handle Custom URL Protocols with the Microsoft WebBrowser Control"},"content":{"rendered":"<p>Now that you know &#8220;<a href=\"http:\/\/www.nuonsoft.com\/blog\/2010\/03\/24\/how-to-use-the-microsoft-webbrowser-control-to-render-html-from-memory\/\">How To Use the Microsoft WebBrowser Control to Render HTML from Memory<\/a>&#8221; and &#8220;<a href=\"http:\/\/www.nuonsoft.com\/blog\/2010\/04\/05\/how-to-navigate-to-an-anchor-in-the-microsoft-webbrowser-control-when-rendering-html-from-memory\/\">How To Navigate to an Anchor in the Microsoft WebBrowser Control when Rendering HTML from Memory<\/a>&#8220;, it&#8217;s time to learn how to handle custom URL protocols to tailor the navigation inside the WebBrowser Control to fit your application. The following demonstrates a custom URL protocol called &#8220;app&#8221;:<\/p>\n<blockquote>\n<pre>&lt;a href=\"app:\/\/some.target\"&gt;Test&lt;\/a&gt;<\/pre>\n<\/blockquote>\n<p>When you would put this link in a normal Internet Explorer window, clicking the link will generate an error because IE does not know how to handle the APP protocol. The name APP is chosen arbitrarily. You can use whatever you want. Handling these custom protocols in your C++ application is actually pretty simple and it doesn&#8217;t even involve any real COM coding like in the previous articles. The first thing you need to do is to add a handler for the BeforeNavigate2\u00a0handler. Open the resource editor and open your dialog with the WebBrowser control. Right click the WebBrowser control and select &#8220;Add Event Handler&#8230;&#8221;. Select \u201cBeforeNavigate2\u201d as message type, select the appropriate class and click \u201cAdd and Edit\u201d. This handler will be called right before the WebBrowser control will navigate to a new page. To handle the custom protocol, implement the handler as follows:<\/p>\n<blockquote>\n<pre>void CMyDlg::BeforeNavigate2Explorer(LPDISPATCH pDisp, VARIANT* URL, VARIANT* Flags,\r\n\u00a0\u00a0\u00a0 VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers, BOOL* Cancel)\r\n{\r\n\u00a0\u00a0\u00a0 const wchar_t* cpszProtocolName = L\"app\";\r\n\u00a0\u00a0\u00a0 const wchar_t* cpszProtocolSeparator = L\":\/\/\";\r\n\r\n\u00a0\u00a0\u00a0 \/\/ We only handle VT_BSTR.\r\n\u00a0\u00a0\u00a0 if (URL-&gt;vt != VT_BSTR)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return;\r\n\r\n\u00a0\u00a0\u00a0 \/\/ Check the protocol of the URL\r\n\u00a0\u00a0\u00a0 CString str = URL-&gt;bstrVal;\r\n\u00a0\u00a0\u00a0 int iPos = str.Find(cpszProtocolSeparator);\r\n\u00a0\u00a0\u00a0 if (iPos == -1)\u00a0\u00a0\u00a0 \/\/ Unable to figure out protocol\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return;\r\n\r\n\u00a0\u00a0\u00a0 \/\/ Extract protocol and check if it's our APP protocol\r\n\u00a0\u00a0\u00a0 CString strProtocol = str.Mid(0, iPos);\r\n\u00a0\u00a0\u00a0 if (strProtocol.CompareNoCase(cpszProtocolName))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return;\u00a0\u00a0\u00a0 \/\/ not our APP protocol\r\n\r\n\u00a0\u00a0\u00a0 \/\/ It's our APP protocol, so start processing it.\r\n\u00a0\u00a0\u00a0 \/\/ Start by preventing Internet Explorer from handling the protocol.\r\n\u00a0\u00a0\u00a0 *Cancel = TRUE;\r\n\r\n\u00a0\u00a0\u00a0 \/\/ Extract target URL\r\n\u00a0\u00a0\u00a0 CString strTarget = str.Mid(iPos+wcslen(cpszProtocolSeparator));\r\n\u00a0\u00a0\u00a0 strTarget.TrimRight(L\"\/\");\r\n\r\n\u00a0\u00a0\u00a0 \/\/ Now we are ready to process our protocol.\r\n\u00a0\u00a0\u00a0 \/\/ For this demo, I just render a new HTML page with the name\r\n\u00a0\u00a0\u00a0 \/\/ of the URL target without the protocol part of the string.\r\n\u00a0\u00a0\u00a0 CString strHTML;\r\n\u00a0\u00a0\u00a0 strHTML.Format(L\"My APP protocol processing: \\\"%s\\\"\", strTarget);\r\n\u00a0\u00a0\u00a0 WriteHTML(strHTML);\r\n}<\/pre>\n<\/blockquote>\n<p>The flow is pretty straightforward. The URL protocol is extracted; if it&#8217;s not our protocol, we let Internet Explorer handle the URL for us. If it is our custom &#8220;APP&#8221; protocol, we first set Cancel to TRUE which will prevent Internet Explorer from handling this URL protocol. Once that is done, we are completely free to implement the handling of the &#8220;APP&#8221; protocol however we want. As demonstration I just write a new HTML document from memory which will just mention that we are processing an &#8220;APP&#8221; protocol URL and that will also display the target part of the URL.<\/p>\n<p>You can quickly test the code with the following piece of <a href=\"http:\/\/www.nuonsoft.com\/blog\/2010\/03\/24\/how-to-use-the-microsoft-webbrowser-control-to-render-html-from-memory\/\">HTML rendered from memory<\/a>:<\/p>\n<blockquote>\n<pre>WriteHTML(L\"&lt;html&gt;&lt;body&gt;\"\r\n\u00a0\u00a0\u00a0 L\"&lt;p&gt;&lt;a href=\\\"app:\/\/FirstAppProtocolTestLink\\\"&gt;test 1&lt;\/a&gt;&lt;\/p&gt;\"\r\n\u00a0\u00a0\u00a0 L\"&lt;p&gt;&lt;a href=\\\"APP:\/\/SecondAppProtocolTestLink.Withdots\\\"&gt;test 2&lt;\/a&gt;&lt;\/p&gt;\"\r\n\u00a0\u00a0\u00a0 L\"&lt;\/body&gt;&lt;\/html&gt;\");<\/pre>\n<\/blockquote>\n<p>Run the application, click on the &#8220;test 1&#8221; or &#8220;test 2&#8221; link and see what happens.<\/p>\n<p>That&#8217;s it for handling custom URL protocols in C++ \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that you know &#8220;How To Use the Microsoft WebBrowser Control to Render HTML from Memory&#8221; and &#8220;How To Navigate to an Anchor in the Microsoft WebBrowser Control when Rendering HTML from Memory&#8220;, it&#8217;s time to learn how to handle custom URL protocols to tailor the navigation inside the WebBrowser Control to fit your application. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,35,7,16,17],"tags":[77,65,64,76,63,62,67],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-c","category-ie","category-mfc","category-microsoft","category-software-development","tag-beforenavigate2","tag-cexplorer","tag-html","tag-url-protocol","tag-web-browser","tag-webbrowser","tag-writehtml"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":4,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/401\/revisions\/405"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}