<?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 GITS Blog &#187; c++</title>
	<atom:link href="http://ginstrom.com/scribbles/tag/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://ginstrom.com/scribbles</link>
	<description>Random scribbling about programming, translation, and Japan</description>
	<lastBuildDate>Fri, 11 May 2012 05:10:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Implementing IDocHostUIHandler in a C++ WTL/ATL project</title>
		<link>http://ginstrom.com/scribbles/2009/11/18/implementing-idochostuihandler/</link>
		<comments>http://ginstrom.com/scribbles/2009/11/18/implementing-idochostuihandler/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 22:41:13 +0000</pubDate>
		<dc:creator>Ryan Ginstrom</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[IDocHostUIHandler]]></category>
		<category><![CDATA[web browser]]></category>
		<category><![CDATA[wtl]]></category>

		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=1370</guid>
		<description><![CDATA[I recently implemented IDocHostUIHandler in one of my WTL projects hosting the web browser. You need to implement this interface if you want to do things like control the context menu of the web browser. I had to go spelunking in various forums and documentation to piece together how to do this, and I never [...]]]></description>
			<content:encoded><![CDATA[<p>I recently implemented <a href="http://msdn.microsoft.com/en-us/library/aa753260%28VS.85%29.aspx">IDocHostUIHandler</a> in one of my <a href="http://wtl.sourceforge.net/">WTL</a> projects hosting the web browser. You need to implement this interface if you want to do things like control the context menu of the web browser.</p>
<p>I had to go spelunking in various forums and documentation to piece together how to do this, and I never found the complete story online, so I thought I'd save any future searchers some trouble. I only did this in Visual Studio 2008, but I've done similar things in VS 2005, so it should work there also.</p>
<p>You should already have a WTL project with an .idl file that implements a COM server. If not, create a new WTL project with the wizard named "MyProject", and choose the option to make it a COM server.</p>
<h3>Add an ATL Simple Object</h3>
<p>Next, add a new class to your project, and select the type "ATL Simple Object."</p>
<p>Here's where you might hit your first snag. You might see the following error dialog:</p>
<div id="attachment_1379" class="wp-caption alignnone" style="width: 496px"><img src="http://ginstrom.com/scribbles/wp-content/uploads/2009/11/err_only_mfc1.png" alt="Error, only MFC projects please" title="Error only MFC" width="486" height="185" class="size-full wp-image-1379" /><p class="wp-caption-text">ATL classes can only be added to MFC EXE and MFC Regular DLL projects or projects with full ATL support</p></div>
<p>"I'm using the ATL, you idiot!" you yell vainly at your computer screen. But alas, the compiler gods did not consider that you might want to use something as esoteric as the WTL for your GUI project, when you have the obviously superior choice of MFC.</p>
<p>To convince Visual Studio that yes, you do deserve to add an ATL Simple Object to your project, you've got to modify the file "VS Root/VC/VCWizards/1033/common.js". Specifically, you've got to have the function <code>IsATLProject</code> return <code>true</code>.</p>
<p>On the Internet, you'll see all sorts of fixes to this function, but I just cut to the chase:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">function</span> IsATLProject<span class="br0">&#40;</span>oProj<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// I promise that I will use this only for good and not evil.</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span> ;<br />
&nbsp; &nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8230;</div>
<p>Try adding the object again; this time, you should be successful. Name your class something descriptive, like "MyHandler" ( <img src='http://ginstrom.com/scribbles/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ), and accept all the defaults.</p>
<h3>Modify the .idl file</h3>
<p>Now go into your .idl file, and make some edits. First, <code>import "atliface.idl"</code>, and <code>#include "olectl.h"</code>. The top of your file should now look like this:</p>
<div class="dean_ch" style="white-space: wrap;">
import <span class="st0">&quot;oaidl.idl&quot;</span>;<br />
import <span class="st0">&quot;ocidl.idl&quot;</span>;<br />
import <span class="st0">&quot;atliface.idl&quot;</span>;</p>
<p><span class="co2">#include &quot;olectl.h&quot;</span></div>
<p>Now, go to your IMyHandler interface, and change the base class from <code>IDispatch</code> to <code>IDocHostUIHandlerDispatch</code>, like so:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="br0">&#91;</span><br />
&nbsp; &nbsp; object,<br />
&nbsp; &nbsp; uuid<span class="br0">&#40;</span><span class="nu0">12345678</span><span class="nu0">-1234</span><span class="nu0">-1234</span><span class="nu0">-1234</span>-123456789ABC<span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; dual,<br />
&nbsp; &nbsp; nonextensible,<br />
&nbsp; &nbsp; helpstring<span class="br0">&#40;</span><span class="st0">&quot;IMyHandler Interface&quot;</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; pointer_default<span class="br0">&#40;</span>unique<span class="br0">&#41;</span><br />
<span class="br0">&#93;</span><br />
<span class="kw1">interface</span> IMyHandler : IDocHostUIHandlerDispatch<span class="br0">&#123;</span><br />
<span class="br0">&#125;</span>;</div>
<h3>Modify your MyHandler.h file</h3>
<p>Go into your CMyHandler class file, and <code>#include "Atliface.h"</code>. Next, add an entry to your COM map for <code>IDocHostUIHandlerDispatch</code>. The top of your file should now look something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#pragma once</span><br />
<span class="co2">#include &quot;resource.h&quot; &nbsp; &nbsp; &nbsp; // main symbols</span><br />
<span class="co2">#include &quot;Atliface.h&quot;</span><br />
<span class="co2">#include &quot;MyProject.h&quot;</span></p>
<p><span class="co2">#if defined(_WIN32_WCE) &amp;&amp; !defined(_CE_DCOM) &amp;&amp; !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)</span><br />
<span class="co2">#error &quot;Blah blah blah&quot;</span><br />
<span class="co2">#endif</span></p>
<p><span class="co1">// CMyHandler</span></p>
<p><span class="kw2">class</span> ATL_NO_VTABLE CMyHandler :<br />
&nbsp; &nbsp; <span class="kw2">public</span> CComObjectRootEx&lt;CComSingleThreadModel&gt;,<br />
&nbsp; &nbsp; <span class="kw2">public</span> CComCoClass&lt;CMyHandler, &amp;CLSID_MyHandler&gt;,<br />
&nbsp; &nbsp; <span class="kw2">public</span> IDispatchImpl&lt;IMyHandler, &amp;IID_MyHandler, &amp;LIBID_MyProject, <span class="coMULTI">/*wMajor =*/</span> <span class="nu0">1</span>, <span class="coMULTI">/*wMinor =*/</span> <span class="nu0">0</span>&gt;<br />
<span class="br0">&#123;</span><br />
<span class="kw2">public</span>:</p>
<p>&nbsp; &nbsp; CMyHandler<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>DECLARE_REGISTRY_RESOURCEID<span class="br0">&#40;</span>IDR_MYHANDLER<span class="br0">&#41;</span></p>
<p>
BEGIN_COM_MAP<span class="br0">&#40;</span>CMyHandler<span class="br0">&#41;</span><br />
&nbsp; &nbsp; COM_INTERFACE_ENTRY<span class="br0">&#40;</span>IMyHandler<span class="br0">&#41;</span><br />
&nbsp; &nbsp; COM_INTERFACE_ENTRY<span class="br0">&#40;</span>IDocHostUIHandlerDispatch<span class="br0">&#41;</span><br />
&nbsp; &nbsp; COM_INTERFACE_ENTRY<span class="br0">&#40;</span>IDispatch<span class="br0">&#41;</span><br />
END_COM_MAP<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
<h3>Add skeletons for the IDocHostUIHandler methods</h3>
<p>Add skeletons for all the methods implemented by <code>IDocHostUIHandler</code>. You can simply copy and paste the method signatures from IDocHostUIHandlerDispatch in "Atliface.h". Now implement the method bodies so that they all return <code>E_NOTIMPL</code>.</p>
<h3>Compile your code</h3>
<p>Now compile, and make sure that everything's working. </p>
<p>Here's where you might hit another snag: by failing to show proper respect to the compiler gods above, you could see an obscure MIDL error when you try to compile. Even if you don't, I recommend compiling your .idl file manually just to be sure. Go to <strong>All Programs</strong> &gt;&gt; <strong>Visual Studio 200X</strong> &gt;&gt; <strong>Visual Studio Tools</strong> &gt;&gt; <strong>Command Prompt</strong> (this loads the command prompt with all your VS symbols defined). </p>
<p>Next, navigate to your source directory, and compile your .idl file:</p>
<div class="dean_ch" style="white-space: wrap;">
midl MyProject.idl</div>
<h3>Implement methods of interest</h3>
<p>Now, you can implement the methods you're interested in. For example, if you want to suppress the context menu, or show your own menu in its place:</p>
<div class="dean_ch" style="white-space: wrap;">
HRESULT STDMETHODCALLTYPE ShowContextMenu<span class="br0">&#40;</span>DWORD dwID, DWORD x, DWORD y, IUnknown *pcmdtReserved, IDispatch *pdispReserved, HRESULT *dwRetVal <span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; *dwRetVal = S_OK;<br />
&nbsp; &nbsp; <span class="co1">// Show your own context menu here if you want.</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> S_OK;<br />
<span class="br0">&#125;</span></div>
<h3>Set the handler in your web browser</h3>
<p>In your OnCreate handler, set your handler in the web browser.</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp; CComObject&lt;CMyHandler&gt; *pUIH = <span class="kw2">NULL</span>;<br />
&nbsp; &nbsp; HRESULT hr = CComObject&lt;CMyHandler&gt;::<span class="me2">CreateInstance</span> <span class="br0">&#40;</span>&amp;pUIH<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>SUCCEEDED<span class="br0">&#40;</span>hr<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Make our custom DocHostUIHandler the window.external handler</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CComQIPtr&lt;IDocHostUIHandlerDispatch&gt; pIUIH = pUIH;<br />
&nbsp; &nbsp; &nbsp; &nbsp; hr = m_view.<span class="me1">SetExternalUIHandler</span><span class="br0">&#40;</span>pIUIH<span class="br0">&#41;</span> ;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; ATLASSERT<span class="br0">&#40;</span>SUCCEEDED<span class="br0">&#40;</span>hr<span class="br0">&#41;</span><span class="br0">&#41;</span> ;</div>
<p>That's it. Compile and run.</p>
]]></content:encoded>
			<wfw:commentRss>http://ginstrom.com/scribbles/2009/11/18/implementing-idochostuihandler/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Coming from C++/Java to Python should bend your mind</title>
		<link>http://ginstrom.com/scribbles/2009/02/03/coming-from-cjava-to-python-should-bend-your-mind/</link>
		<comments>http://ginstrom.com/scribbles/2009/02/03/coming-from-cjava-to-python-should-bend-your-mind/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 02:30:38 +0000</pubDate>
		<dc:creator>Ryan Ginstrom</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pythonic]]></category>

		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=839</guid>
		<description><![CDATA[Coming from C++ or Java to Python should bend your mind. If it doesn't, then you haven't learned Python yet &#8212; you're just writing C++ or Java in Python. If you only knew a statically typed and compiled language like C++ or Java before, and learning Python hasn't changed the way you think about programming, [...]]]></description>
			<content:encoded><![CDATA[<p>Coming from C++ or Java to Python should bend your mind. If it doesn't, then you haven't learned Python yet &#8212; you're just writing C++ or Java in Python.</p>
<p>If you only knew a statically typed and compiled language like C++ or Java before, and learning Python hasn't changed the way you think about programming, then check your programs for lots of type-checking code, inheritance, array indexing, and mutable variables. Look at some <a href="http://effbot.org/">excellent</a> <a href="http://www.aleax.it/python_mat_en.html">Python</a> <a href="http://code.activestate.com/recipes/">code</a>, and identify where your code diverges from it.</p>
]]></content:encoded>
			<wfw:commentRss>http://ginstrom.com/scribbles/2009/02/03/coming-from-cjava-to-python-should-bend-your-mind/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

