<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>the kiZZ notes &#187; BHO</title>
	<atom:link href="http://kizzx2.com/blog/index.php/tag/bho/feed/" rel="self" type="application/rss+xml" />
	<link>http://kizzx2.com/blog</link>
	<description>random stupid ideas from a narcissistic chode</description>
	<lastBuildDate>Thu, 01 Dec 2011 16:25:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>K2HttpRequest: asynchronous HTTP request using IWebBrowser2</title>
		<link>http://kizzx2.com/blog/index.php/2008/05/05/k2httprequest-asynchronous-http-request-using-iwebbrowser2/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/05/05/k2httprequest-asynchronous-http-request-using-iwebbrowser2/#comments</comments>
		<pubDate>Mon, 05 May 2008 15:34:01 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[BHO]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=47</guid>
		<description><![CDATA[In my previous battle against Internet Explorer add-on (BHO), I discovered some valuable (and scarce) resources to my aid. Pete&#8216;s TinyBHO and CPeteHttpRequest probably saved the most of my day. Then I realized a problem: for the browser add-on that I was writing, I would have to fetch a Web page from a server that [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://kizzx2.com/blog/?p=45">previous battle</a> against Internet Explorer add-on (BHO), I discovered some valuable (and scarce) resources to my aid. <a href="http://petewarden.typepad.com">Pete</a>&#8216;s <a href="http://petesearch.com/wiki/index.php/SimpleBho">TinyBHO</a> and <a href="http://petewarden.typepad.com/searchbrowser/2007/06/xmlhttprequest__1.html">CPeteHttpRequest</a> probably saved the most of my day.</p>

<p>Then I realized a problem: for the browser add-on that I was writing, I would have to fetch a Web page from a server that requires user login. Under typical usage scenarios, the users are assumed to have logged in the server in a previous session, so the Web server will authorize the user from cookies. However, using <code>WinHttp</code> on which <code>CPeteHttpRequest</code> is based on, there seems to be no (simple) ways to obtain the user&#8217;s cookies and embed it in the HTTP request. I couldn&#8217;t find another piece that is as simplistic as <code>CPeteHttpRequest</code> and also able to deal with my problem at hand.</p>

<p>So I wrote one myself.</p>

<p><code>K2HttpRequest</code> has the usage of:</p>

<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">K2HttpRequest request<span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Synchronous</span><br />
wstring innerHTML <span style="color: #008000;">=</span> request<span style="color: #008000;">.</span><span style="color: #0000FF;">request</span> <span style="color: #008000;">&#40;</span> L<span style="color: #666666;">&quot;http://www.google.com&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Asynchronous</span><br />
<span style="color: #6666cc; font-weight: bold;">void</span> foo <span style="color: #008000;">&#40;</span> wstring innerHTML <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
request<span style="color: #008000;">.</span><span style="color: #0000FF;">SetOnComplete</span> <span style="color: #008000;">&#40;</span> foo <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
request<span style="color: #008000;">.</span><span style="color: #0000FF;">request</span> <span style="color: #008000;">&#40;</span> L<span style="color: #666666;">&quot;http://www.google.com&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>

<p><code>K2HttpRequest</code> uses <code>IWebBrowser2</code> internally, which means it opens a new Internet Explorer instance under the hood and loads the Web page with it. Thus, the server will receive login cookies as if the user visited the Web page. It will fetch the Web page at the specified URL and return the HTML in the <code>&amp;lt;body&amp;gt;</code> element (I actually intended return the whole <code>&amp;lt;html&amp;gt;</code> element but I can&#8217;t find an easy way to do it, as <code>IHTMLDocument2</code> doesn&#8217;t have <code>get_innerHTML()</code>)</p>

<p>This is still a very preliminary version, and will probably improve as my COM/ATL skills improve some day.</p>

<p class="download"><a title="k2httprequest.zip" href="http://kizzx2.com/blog/wp-content/uploads/2008/05/k2httprequest.zip">Download K2HttpRequest</a>(source code ~5KB, Visual C++ 2008)</p>

<p class="download"><em>(Update: The asynchronous part of this doesn&#8217;t work very well. I didn&#8217;t know COM enough to use Marshalling when I wrote this version. I&#8217;ll probably update this some day. If you want to get the asynchrounous part working, you&#8217;ll probably have to add in marshalling yourself :p)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/05/05/k2httprequest-asynchronous-http-request-using-iwebbrowser2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>making an Internet Explorer add-on</title>
		<link>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/#comments</comments>
		<pubDate>Thu, 01 May 2008 13:37:02 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[drumming]]></category>
		<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[add-on]]></category>
		<category><![CDATA[BHO]]></category>
		<category><![CDATA[browser helper object]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=45</guid>
		<description><![CDATA[I was recently doing a project to make an add-on for Internet Explorer. How do I put it? I must say that was an extremely frustrating experience. The documentation is severely lacking, if not non-existent. One page from MSDN (which states the psarray is a BSTR is even dead wrong). I have never been more [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently doing a project to make an add-on for Internet Explorer. How do I put it? I must say that was an extremely frustrating experience. The documentation is severely lacking, if not non-existent. <a href="http://msdn.microsoft.com/en-us/library/aa752639(VS.85).aspx">One page from MSDN</a> (which states the <code>psarray</code> is a <code>BSTR</code> is even dead wrong).</p>

<p>I have never been more motivated to contribute to the scene by writing my own &#8220;Internet Explorer add-on writing survival guide&#8221; sort of thing, but then I realized <a href="http://petewarden.typepad.com/">Pete</a> has beat me to it by opening an entire [bho-wiki][wiki].</p>

<p>Nevertheless, I&#8217;m writing this article so more people can find these scarce information on IE add-on. On the other hand, here are some thoughts I had when writing the IE add-on. If you&#8217;re about to write an IE add-on, these would probably be the &#8220;survival guides&#8221; you&#8217;re using (as there aren&#8217;t many).</p>

<p><strong>1. An IE add-on is a Browser Helper Object (BHO)</strong></p>

<p>To save you some time searching in vain, try using BHO in your Google searches. This would give you programming-related information instead of where to download the latest IE add-on product.</p>

<p><strong>2. Use C#</strong></p>

<p>C++ just plain sucks, in terms of writing an IE add-on. Don&#8217;t get me wrong here, the language is marvelous, but Microsoft&#8217;s support just seems broken and unfinished. The whole Win32 hacking, COM stuffs involved when using C++ just makes your life 10 times more difficult.</p>

<p>The interface for C# is much more usable, and you won&#8217;t have to deal with all the bullshits when programming in the outdated (and most probably unmaintained) Win32 API.</p>

<p>Another major issue with C++ is Visual C++ 2008 Express. It doesn&#8217;t come with ATL, which means a ridiculous amount of grunt work dealing with pointers, releasing and stuffs (as illustrated below, as I happened to use VC++ Express). VC# doesn&#8217;t have this problem and it even has a number of libraries built-in (such as regular expressions).</p>

<p><em>Example 1: getElementById from BHO</em></p>

<p><em>Using C++</em></p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">IHTMLCollection <span style="color: #000040;">*</span> elements<span style="color: #008080;">;</span><br />
HRESULT hr <span style="color: #000080;">=</span> document<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>get_all <span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>elements <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">long</span> numElements<span style="color: #008080;">;</span><br />
hr <span style="color: #000080;">=</span> elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>get_length <span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>numElements <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
IHTMLElement <span style="color: #000040;">*</span> element<span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> numElements<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
VARIANT index<span style="color: #008080;">;</span><br />
index.<span style="color: #007788;">vt</span> <span style="color: #000080;">=</span> VT_I4<span style="color: #008080;">;</span><br />
index.<span style="color: #007788;">intVal</span> <span style="color: #000080;">=</span> i<span style="color: #008080;">;</span><br />
<br />
IDispatch <span style="color: #000040;">*</span> elementDispatch<span style="color: #008080;">;</span><br />
<br />
hr <span style="color: #000080;">=</span> elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>item <span style="color: #008000;">&#40;</span> index, index, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>elementDispatch <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
hr <span style="color: #000080;">=</span> elementDispatch<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>QueryInterface <span style="color: #008000;">&#40;</span> IID_IHTMLElement, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>element <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
elementDispatch<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// After all this hard work, we have finally obtained our element!</span><br />
<span style="color: #666666;">// GetAttribute is a custom function, I didn't even bother to</span><br />
<span style="color: #666666;">// list out it's content here</span><br />
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> GetAttribute <span style="color: #008000;">&#40;</span> element, L<span style="color: #FF0000;">&quot;id&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> id <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> element<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> element <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">return</span> element<span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p><em>Using C#</em></p>

<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">document<span style="color: #008000;">.</span><span style="color: #0000FF;">getElementById</span><span style="color: #008000;">&#40;</span>id<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>

<p><em>Example 2: execute some Javascript</em></p>

<p><em>Using C++</em></p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">BSTR code <span style="color: #000080;">=</span> SysAllocString <span style="color: #008000;">&#40;</span> L<span style="color: #FF0000;">&quot;alert ( 'Hello World' );&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
BSTR language <span style="color: #000080;">=</span> SysAllocString <span style="color: #008000;">&#40;</span> L<span style="color: #FF0000;">&quot;javascript&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
VARIANT result <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
window<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>execScript <span style="color: #008000;">&#40;</span> code, language, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>result <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
SysFreeString <span style="color: #008000;">&#40;</span> code <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
SysFreeString <span style="color: #008000;">&#40;</span> language <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p><em>Using C#</em></p>

<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window<span style="color: #008000;">.</span><span style="color: #0000FF;">execScript</span> <span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;alert ( 'Hello World' )&quot;</span>, <span style="color: #666666;">&quot;javascript&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>

<p><strong>3. Use jQuery for DOM manipulation</strong></p>

<p><a href="http://jquery.com">jQuery</a> is a client side JavaScript library to simplify common tasks (the &#8220;write less, do more&#8221; library as they say). I found it delightfully pleasant to use. It has some powerful functions like CSS selectors that would be a pain in the ass to implement in BHO. With jQuery, you can do stuff like:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Find all the &amp;lt;a&amp;gt; elements with class &quot;bookmark&quot; and</span><br />
<span style="color: #006600; font-style: italic;">// target attribute &quot;_blank&quot; and hide</span><br />
<span style="color: #006600; font-style: italic;">// them smoothly using a slow slide up animation</span><br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.bookmark[@target=&quot;_blank&quot;]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>To use jQuery, simply use <code>execScript</code> to load the jQuery file on document complete.</p>

<p><strong>4. About XmlHttpRequest
</strong></p>

<p>Fetching external Web pages using client side JavaScript will give you a permission denied error because of the cross-domain policies of most modern browsers. XmlHttpRequest must be done in the BHO. <a href="http://www.ookii.org/">Sven</a> offers <a href="http://www.ookii.org/post/using_ixmlhttprequestonreadystatechange_from_c.aspx">an example code</a> to use IXmlHttpRequest, which is very valuable considering the lack of official documentation. Pete has rolled his own <a href="http://petewarden.typepad.com/searchbrowser/2007/06/xmlhttprequest__1.html">CPeteHttpRequest</a> class using WinHttp.</p>

<p>(Strangely enough, both of the above are coded in C++. I have yet to come across an equally efficient way to do XmlHttpRequest in C#. Please feel free to leave a comment if you have good ideas)</p>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

