<?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>Stuart Jones &#187; Development</title>
	<atom:link href="http://gingerbbm.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://gingerbbm.com</link>
	<description>User Experience Design &#38; Software Development</description>
	<lastBuildDate>Mon, 05 Dec 2011 23:13:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Add items to a Word document using C#</title>
		<link>http://gingerbbm.com/2010/06/add-items-to-a-word-document-using-c/</link>
		<comments>http://gingerbbm.com/2010/06/add-items-to-a-word-document-using-c/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 23:28:43 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Word Automation]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[endofdoc]]></category>
		<category><![CDATA[word]]></category>
		<category><![CDATA[word interop]]></category>
		<category><![CDATA[Word.Range]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=362</guid>
		<description><![CDATA[I thought I had found the promised land. Then I tried adding more than one item to a blank Word document using C# and I realised it had been a mirage all along. I simply wanted to create a blank &#8230; <a href="http://gingerbbm.com/2010/06/add-items-to-a-word-document-using-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://gingerbbm.com/2010/06/add-a-table-using-word-interop-and-converttotable/">I thought I had found the promised land</a>. Then I tried adding more than one item to a blank Word document using C# and I realised it had been a mirage all along. I simply wanted to create a blank document, add a line of text, followed by another, followed by my table. I was flummoxed by the <a href="http://msdn.microsoft.com/es-es/library/ms264372(v=Office.11).aspx">Word.Range</a> object &#8211; and I&#8217;ll go into detail in a bit &#8211; but here&#8217;s the solution:<span id="more-362"></span></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">object oEndOfDoc <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>endofdoc&quot;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* \endofdoc is a predefined bookmark */</span>
&nbsp;
Word.<span style="color: #202020;">Application</span> app <span style="color: #339933;">=</span> new Word.<span style="color: #202020;">Application</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Word.<span style="color: #202020;">Document</span> doc <span style="color: #339933;">=</span> app.<span style="color: #202020;">Documents</span>.<span style="color: #202020;">Add</span><span style="color: #009900;">&#40;</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Word.<span style="color: #202020;">Range</span> line1 <span style="color: #339933;">=</span> doc.<span style="color: #202020;">Bookmarks</span>.<span style="color: #202020;">Item</span><span style="color: #009900;">&#40;</span>ref oEndOfDoc<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">Range</span><span style="color: #339933;">;</span>
line1.<span style="color: #202020;">Text</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;First line!&quot;</span><span style="color: #339933;">;</span>
line1.<span style="color: #202020;">InsertParagraphAfter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Word.<span style="color: #202020;">Range</span> line2 <span style="color: #339933;">=</span> doc.<span style="color: #202020;">Bookmarks</span>.<span style="color: #202020;">Item</span><span style="color: #009900;">&#40;</span>ref oEndOfDoc<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">Range</span><span style="color: #339933;">;</span>
line2.<span style="color: #202020;">Text</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Second line!&quot;</span><span style="color: #339933;">;</span>
line2.<span style="color: #202020;">InsertParagraphAfter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Word.<span style="color: #202020;">Range</span> r <span style="color: #339933;">=</span> doc.<span style="color: #202020;">Bookmarks</span>.<span style="color: #202020;">Item</span><span style="color: #009900;">&#40;</span>ref oEndOfDoc<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">Range</span><span style="color: #339933;">;</span>
r.<span style="color: #202020;">Text</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Name<span style="color: #000099; font-weight: bold;">\t</span>Age<span style="color: #000099; font-weight: bold;">\t</span>Location<span style="color: #000099; font-weight: bold;">\n</span>Nigel<span style="color: #000099; font-weight: bold;">\t</span>44<span style="color: #000099; font-weight: bold;">\t</span>UK<span style="color: #000099; font-weight: bold;">\n</span>Bill<span style="color: #000099; font-weight: bold;">\t</span>33<span style="color: #000099; font-weight: bold;">\t</span>USA<span style="color: #000099; font-weight: bold;">\n</span>Ruben<span style="color: #000099; font-weight: bold;">\t</span>86<span style="color: #000099; font-weight: bold;">\t</span>Nicaragua<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
object tab <span style="color: #339933;">=</span> Word.<span style="color: #202020;">WdTableFieldSeparator</span>.<span style="color: #202020;">wdSeparateByTabs</span><span style="color: #339933;">;</span>
Word.<span style="color: #202020;">Table</span> t  <span style="color: #339933;">=</span> r.<span style="color: #202020;">ConvertToTable</span><span style="color: #009900;">&#40;</span>
    ref tab<span style="color: #339933;">,</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Looks pretty obvious, doesn&#8217;t it? But it took me a while to get there. The important difference is that instead of&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Word.<span style="color: #202020;">Range</span> r <span style="color: #339933;">=</span> doc.<span style="color: #202020;">Range</span><span style="color: #009900;">&#40;</span>ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;I used:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Word.<span style="color: #202020;">Range</span> r <span style="color: #339933;">=</span> doc.<span style="color: #202020;">Bookmarks</span>.<span style="color: #202020;">Item</span><span style="color: #009900;">&#40;</span>ref oEndOfDoc<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">Range</span><span style="color: #339933;">;</span></pre></div></div>

<p>What I had been experiencing with the former was a document with only the final item (the table) being added. In fact, all items had been added, but at the same location in the document &#8211; so each item was being overwritten by each subsequent item. This seems obvious too, in retrospect, because I was supplying the <em><a href="http://msdn.microsoft.com/en-us/library/system.reflection.missing.value.aspx">missing</a></em> value to the Range constructor&#8217;s <em>start</em> and <em>end</em> parameters, so the target range became &#8220;everything&#8221;.</p>
<p>I couldn&#8217;t see &#8211; and still can&#8217;t &#8211; how you&#8217;re supposed to identify a range in an empty document, but I was fortunate enough to discover <em>endofdoc</em>, a built-in bookmark signifying the, er, end of a document.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">object oEndOfDoc <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>endofdoc&quot;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* \endofdoc is a predefined bookmark */</span></pre></div></div>

<p>My requirement was simply to add items to the document sequentially, so using this bookmark for the range of each item did the trick. I think that&#8217;s me done with Word Automation&#8230;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>P.S.</strong> In trying to find a solution I toyed with a Word document template with predefined mail merge fields acting as placeholders for the items I wanted to add. It almost worked until I tried to replace a field with my table. Any ideas why this doesn&#8217;t work?</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">foreach <span style="color: #009900;">&#40;</span>Word.<span style="color: #202020;">Field</span> myMergeField in doc.<span style="color: #202020;">Fields</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
    myMergeField.<span style="color: #202020;">Select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    app.<span style="color: #202020;">Selection</span>.<span style="color: #202020;">Range</span>.<span style="color: #202020;">Text</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Name<span style="color: #000099; font-weight: bold;">\t</span>Age<span style="color: #000099; font-weight: bold;">\t</span>Location<span style="color: #000099; font-weight: bold;">\n</span>Nigel<span style="color: #000099; font-weight: bold;">\t</span>44<span style="color: #000099; font-weight: bold;">\t</span>UK<span style="color: #000099; font-weight: bold;">\n</span>Bill<span style="color: #000099; font-weight: bold;">\t</span>33<span style="color: #000099; font-weight: bold;">\t</span>USA<span style="color: #000099; font-weight: bold;">\n</span>Ruben<span style="color: #000099; font-weight: bold;">\t</span>86<span style="color: #000099; font-weight: bold;">\t</span>Nicaragua<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    app.<span style="color: #202020;">Selection</span>.<span style="color: #202020;">Range</span>.<span style="color: #202020;">ConvertToTable</span><span style="color: #009900;">&#40;</span>
        ref tab<span style="color: #339933;">,</span>
        ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
        ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
        ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
        ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2010/06/add-items-to-a-word-document-using-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Add a table using Word Interop and ConvertToTable</title>
		<link>http://gingerbbm.com/2010/06/add-a-table-using-word-interop-and-converttotable/</link>
		<comments>http://gingerbbm.com/2010/06/add-a-table-using-word-interop-and-converttotable/#comments</comments>
		<pubDate>Mon, 31 May 2010 23:43:56 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Word Automation]]></category>
		<category><![CDATA[add table]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[converttotable]]></category>
		<category><![CDATA[word]]></category>
		<category><![CDATA[word interop]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=342</guid>
		<description><![CDATA[The .NET Framework allows us to do many things with relative ease but the main cost is often the time taken in finding a decent example to learn from. I experienced pain when I wanted to create a Word document &#8230; <a href="http://gingerbbm.com/2010/06/add-a-table-using-word-interop-and-converttotable/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The .NET Framework allows us to do many things with relative ease but the main cost is often the time taken in finding a decent example to learn from. I experienced pain when I wanted to create a Word document programmatically and add a simple table to it. But I got there in the end, and this is how I did it. <span id="more-342"></span></p>
<p>I&#8217;m not going to describe in full how you need to prepare your Visual Studio project. For that, read <a href="http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx">this</a>. I wanted to utilise the <a href="http://msdn.microsoft.com/es-es/library/microsoft.office.interop.word.range.converttotable(office.11).aspx">ConvertToTable()</a> method of the <a href="http://msdn.microsoft.com/es-es/library/ms264372(v=Office.11).aspx">Word.Range</a> object which allows us to create a table using a string delimited by tabs and newlines such as this:</p>
<div style="font-family: courier new;"><span style="color: red;">Name</span><span style="color: blue;">\t</span><span style="color: red;">Age</span><span style="color: blue;">\t</span><span style="color: red;">Location</span><span style="color: blue;">\n</span><span style="color: red;">Nigel</span><span style="color: blue;">\t</span><span style="color: red;">44</span><span style="color: blue;">\t</span><span style="color: red;">UK</span><span style="color: blue;">\n</span><span style="color: red;">Bill</span><span style="color: blue;">\t</span><span style="color: red;">33</span><span style="color: blue;">\t</span><span style="color: red;">USA</span><span style="color: blue;">\n</span><span style="color: red;">Ruben</span><span style="color: blue;">\t</span><span style="color: red;">86</span><span style="color: blue;">\t</span><span style="color: red;">Nicaragua</span><span style="color: blue;">\n</span>&nbsp;</p>
</div>
<p>The MSDN page <a href="http://msdn.microsoft.com/en-us/library/aa537149(office.11).aspx">Automating Word Tables for Data Insertion and Extraction</a> had taken me so far, but not all the way, so here&#8217;s the useful (read: missing) code to bring it all together:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Word.<span style="color: #202020;">Application</span> app <span style="color: #339933;">=</span> new Word.<span style="color: #202020;">Application</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Word.<span style="color: #202020;">Document</span> doc <span style="color: #339933;">=</span> app.<span style="color: #202020;">Documents</span>.<span style="color: #202020;">Add</span><span style="color: #009900;">&#40;</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Word.<span style="color: #202020;">Range</span> r <span style="color: #339933;">=</span> doc.<span style="color: #202020;">Range</span><span style="color: #009900;">&#40;</span>ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
r.<span style="color: #202020;">Text</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Name<span style="color: #000099; font-weight: bold;">\t</span>Age<span style="color: #000099; font-weight: bold;">\t</span>Location<span style="color: #000099; font-weight: bold;">\n</span>Nigel<span style="color: #000099; font-weight: bold;">\t</span>44<span style="color: #000099; font-weight: bold;">\t</span>UK<span style="color: #000099; font-weight: bold;">\n</span>Bill<span style="color: #000099; font-weight: bold;">\t</span>33<span style="color: #000099; font-weight: bold;">\t</span>USA<span style="color: #000099; font-weight: bold;">\n</span>Ruben<span style="color: #000099; font-weight: bold;">\t</span>86<span style="color: #000099; font-weight: bold;">\t</span>Nicaragua<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
object tab <span style="color: #339933;">=</span> Word.<span style="color: #202020;">WdTableFieldSeparator</span>.<span style="color: #202020;">wdSeparateByTabs</span><span style="color: #339933;">;</span>
Word.<span style="color: #202020;">Table</span> t  <span style="color: #339933;">=</span> r.<span style="color: #202020;">ConvertToTable</span><span style="color: #009900;">&#40;</span>
    ref tab<span style="color: #339933;">,</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span>
    ref oMissing<span style="color: #339933;">,</span> ref oMissing<span style="color: #339933;">,</span> ref oMissing
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Please leave a comment if you have any questions on this, or if I&#8217;ve done something heinous that is a crime against Word automation <img src='http://gingerbbm.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Good luck!</p>
<p>&nbsp;</p>
<p><strong>EDIT:</strong> If you need to add more than one item to a blank Word document, you might want to take a look at <a href="http://gingerbbm.com/2010/06/add-items-to-a-word-document-using-c/">my subsequent post</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2010/06/add-a-table-using-word-interop-and-converttotable/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>IE6 Compatibility VPC IETest Password Is P2ssw0rd</title>
		<link>http://gingerbbm.com/2010/04/ie6-compatibility-vpc-ietest-password-is-p2ssw0rd/</link>
		<comments>http://gingerbbm.com/2010/04/ie6-compatibility-vpc-ietest-password-is-p2ssw0rd/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 12:18:25 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[compatibility]]></category>
		<category><![CDATA[ietest]]></category>
		<category><![CDATA[P2ssw0rd]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[vpc]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=338</guid>
		<description><![CDATA[I just downloaded and installed the very useful IE6 Compatibility Virtual PC [IE6-on-XP-SP3.exe] from Microsoft. Within minutes of starting the machine and getting distracted, the screensaver kicked in, locking the machine until I entered the relevant password for the IETest &#8230; <a href="http://gingerbbm.com/2010/04/ie6-compatibility-vpc-ietest-password-is-p2ssw0rd/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I just downloaded and installed the very useful IE6 Compatibility Virtual PC [IE6-on-XP-SP3.exe] from <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=21EABB90-958F-4B64-B5F1-73D0A413C8EF&#038;displaylang=en">Microsoft</a>. Within minutes of starting the machine and getting distracted, the screensaver kicked in, locking the machine until I entered the relevant password for the 	<em>IETest</em> user. The password is <strong>P2ssw0rd</strong>. <span id="more-338"></span></p>
<p>This information wasn&#8217;t immediately obvious via Google so I thought I&#8217;d post it up in an attempt to make it more findable by other folks. I actually got the information from <a href="http://www.spazquest.org/weblog/2009/11/password-for-ie6-app-compatibility-vpc.html">Eric True</a>. Thanks Eric!</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2010/04/ie6-compatibility-vpc-ietest-password-is-p2ssw0rd/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>URL Editor</title>
		<link>http://gingerbbm.com/2009/09/url-editor/</link>
		<comments>http://gingerbbm.com/2009/09/url-editor/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 22:21:29 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[http get]]></category>
		<category><![CDATA[long url]]></category>
		<category><![CDATA[querystring]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[url editor]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=207</guid>
		<description><![CDATA[On a few occasions recently I&#8217;ve had to deal with long URLs, or more accurately, URLs with long querystrings. Each time it was a chore: strings that are a few hundred characters in length are unwieldy, hard to read and &#8230; <a href="http://gingerbbm.com/2009/09/url-editor/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>On a few occasions recently I&#8217;ve had to deal with long URLs, or more accurately, URLs with long <a href="http://en.wikipedia.org/wiki/Query_string" target="_blank">querystrings</a>. Each time it was a chore: strings that are a few hundred characters in length are unwieldy, hard to read and tricky to edit. So I decided to make my life easier and build a tool. <span id="more-207"></span></p>
<p>This URL is the kind of thing one encounters when working with Web Map Services:</p>
<div style="margin-left:30px;margin-right:30px;margin-bottom:20px;font-family:courier new,courier;color:blue;border:1px solid #9C9A9C;background-color:#F7F3EF;padding:10px">http://terraservice.net/ogcmap.ashx?SERVICE=WMS&amp;REQUEST=GetMap&amp;VERSION=1.1.1&amp;LAYERS=DRG&amp;FORMAT=image/gif&amp;SRS=EPSG:4326&amp;BBOX=-124.1,41.2,-123.9,41.4&amp;WIDTH=300&amp;HEIGHT=300</div>
<p>It returns a geographic map image based upon the name/value pairs passed in the querystring. <a href="http://code.google.com/apis/chart/" target="_blank">Google Charts</a> work in a similar way:</p>
<div style="margin-left:30px;margin-right:30px;margin-bottom:20px;font-family:courier new,courier;color:blue;border:1px solid #9C9A9C;background-color:#F7F3EF;padding:10px">http://chart.apis.google.com/chart?cht=lc&amp;chd=s:cEAELFJHHHKUju9uuXUc&amp;chco=76A4FB&amp;chls=2.0,0.0,0.0&amp;chs=200&#215;125&amp;chg=20,50,3,3,10,20&amp;chxt=x,y&amp;chxl= 0:|0|1|2|3|4|5|1:|0|50|100</div>
<p>To tackle such long URLs I&#8217;ve developed the boringly-named <strong>URL Editor</strong>.</p>
<p>Find it here: <a href="http://gingerbbm.com/urleditor" target="_blank">http://gingerbbm.com/urleditor</a></p>
<p>It&#8217;s a pretty simple JavaScript application (using Prototype and Scriptaculous) whose main purpose is to split the long querystring of a URL into its constituent name/value pairs in order to dramatically improve legibility. Paste a URL into the main box, click <em>Split</em>, and the name/value pairs appear below:</p>
<p><a href="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/urleditor01.png"><img class="alignnone size-full wp-image-243" title="urleditor01" src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/urleditor01.png" alt="urleditor01" width="500" /></a></p>
<p>Once a URL has been split, any edits to the individual values are immediately applied to the main URL, and vice versa.</p>
<p>The <em>Encode</em> and <em>Decode</em> buttons attempt to <em>encode/decode</em> any relevant values that exist.</p>
<p><a href="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/urleditor02.png"><img class="alignnone size-full wp-image-244" title="urleditor02" src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/urleditor02.png" alt="urleditor02" width="500" /></a></p>
<p>Click <em>Open</em> to open the URL in its current state in a new window/tab. And <em>Reset</em> resets the whole page.</p>
<p>Reordering of name/value pairs is achieved by dragging them, and you can add and delete them by clicking the relevant buttons.</p>
<h2>Bugs</h2>
<p>It wouldn&#8217;t be hard to trip the application up. The key to it all is the question mark character that denotes the start of a querystring. If you&#8217;re not interested in ever <em>opening</em> the URL you can simply start typing into the main box, e.g. &#8220;?a=1&#8243;, and the name/value pairs will appear.</p>
<h2>Names</h2>
<p>So, &#8220;URL Editor&#8221; is a fairly sober moniker, but it was the best of a bad bunch, and being a pretty good description of what it does I decided to go with it.</p>
<ul>
<li>U.R.[NOT HEL]L.</li>
<li>URL Manager</li>
<li>URL Splitter</li>
<li>Querystring Dechunker</li>
<li>Any ideas?</li>
</ul>
<h2>And Finally</h2>
<p>I&#8217;ve tried it out in FF3, IE8, Safari 3 and Opera 9 on XP and it&#8217;s all good. It also appears to work in FF3 and Safari 3 on the Mac. Not tried it on Chrome yet, but I don&#8217;t expect any surprises. For me it&#8217;s been fairly robust, and a useful tool in my arsenal. I hope others find it useful.</p>
<p> <img src='http://gingerbbm.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/09/url-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Path and Filename to Clipboard</title>
		<link>http://gingerbbm.com/2009/09/path-and-filename-to-clipboard/</link>
		<comments>http://gingerbbm.com/2009/09/path-and-filename-to-clipboard/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 11:55:26 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[helper app]]></category>
		<category><![CDATA[xp]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=196</guid>
		<description><![CDATA[Many moons ago I wrote a little helper application for Windows that would allow you to right-click a file in Explorer and get its full path, including filename, into the clipboard. Owing to underwhelming demand I have resurrected it. Get &#8230; <a href="http://gingerbbm.com/2009/09/path-and-filename-to-clipboard/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Many moons ago I wrote a little helper application for Windows that would allow you to right-click a file in Explorer and get its full path, including filename, into the clipboard. Owing to underwhelming demand I have resurrected it. <span id="more-196"></span></p>
<p>Get it here:<br />
<a href="http://gingerbbm.com/software/PathAndFilenameToClipboardSetup.msi">http://gingerbbm.com/software/PathAndFilenameToClipboardSetup.msi</a></p>
<p>It adds itself to the context menu in Explorer and appears like so:</p>
<p><img src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/09/paftc.png" alt="Path and Filename to Clipboard" title="Path and Filename to Clipboard" width="397" height="477" class="size-full wp-image-201" /></p>
<p><strong>Disclaimer:</strong> Of course this software comes with absolutely no guarantees or warranties of any kind! It should work on Windows XP; I&#8217;ve not tried it on anything else. If you have any difficulties leave a comment and I might take a look&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/09/path-and-filename-to-clipboard/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HTML images using long GET strings</title>
		<link>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/</link>
		<comments>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 21:15:13 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[http get]]></category>
		<category><![CDATA[http post]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[long url]]></category>
		<category><![CDATA[wms]]></category>

		<guid isPermaLink="false">http://gingerbbm.com/?p=127</guid>
		<description><![CDATA[The Problem You&#8217;ve got a piece of code that creates an image in your web page by passing a bunch of parameters in a GET string to some server-side process: $&#40;'tile'&#41;.src = 'http://img.com/generate.php?a=1&#38;amp;b=2&#38;amp;c=3'; But what do you do when the &#8230; <a href="http://gingerbbm.com/2009/06/html-images-using-long-get-strings/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>The Problem</h2>
<p>You&#8217;ve got a piece of code that creates an image in your web page by passing a bunch of parameters in a GET string to some server-side process:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://img.com/generate.php?a=1&amp;amp;b=2&amp;amp;c=3'</span><span style="color: #339933;">;</span></pre></div></div>

<p>But what do you do when the URL goes <a href="http://www.boutell.com/newfaq/misc/urllength.html" target="_blank">beyond the limit</a>? Practically, for cross-browser solutions, the limit is around 2000 characters (thanks to IE), and I recently had to find a workaround when generating images via a <a href="http://en.wikipedia.org/wiki/Web_Map_Service" target="_blank">WMS</a>.<span id="more-127"></span></p>
<h2>The Solution</h2>
<p>When we&#8217;re dealing with HTML forms, if a GET isn&#8217;t big enough we have to use a POST. The difficulty in the scenario described above is that there&#8217;s no way to provide the POST variables when making the assignment to the image&#8217;s <em>src</em> attribute.</p>
<p>I thought about how to use an Ajax POST but even if image data could be streamed back to the browser in this way, there&#8217;s no mechanism for getting it into an HTML image.</p>
<p>And then I discovered the <a href="http://en.wikipedia.org/wiki/Data_URI_scheme" target="_blank">data URI scheme</a> which allows us to assign a <a href="http://en.wikipedia.org/wiki/Base64" target="_blank">base64</a> representation of an image to an HTML image like so (where the ellipsis indicates the position of the base64 data string):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'data:image/gif;base64,...'</span><span style="color: #339933;">;</span></pre></div></div>

<p>If I could issue a POST to the server to generate my image, I could convert that image into a base64 string to pass back to the browser, which I could then utilise in the data URI scheme syntax.</p>
<p>I discovered this method in <a href="http://www.phpied.com/data-urls-what-are-they-and-how-to-use/" target="_blank">this brilliant post</a> where the same trick is used to for CSS and to reduce HTTP requests. And by reading that post I learned that <a href="http://www.phpied.com/mhtml-when-you-need-data-uris-in-ie7-and-under/" target="_blank">we have to do it another way in IE</a>. In IE we have to use <a href="http://en.wikipedia.org/wiki/MHTML" target="_blank">MHTML</a> and it involves a little more work on the server but we can still end up with this (where <em>somestring</em> indicates the position in <em>mhtml.txt</em> of the base64 data string):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tile'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'mhtml:http://img.com/mhtml.txt!somestring'</span><span style="color: #339933;">;</span></pre></div></div>

<p>A proxy script on the server is required to pass the POST variables through to the image-generation program, and convert the resultant image into a base64 data string. Since the WMS I use is located on an external server I was already using <a href="http://www.troywolf.com/articles/php/class_http/proxy.phps" target="_blank">Troy Wolf&#8217;s PHP proxy script</a> (note that it requires <em><a href="http://www.troywolf.com/articles/php/class_http/class_http.phps" target="_blank">class.php</a></em>). Converting to base64 is a cinch in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$base64_encoded_image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I simply added the code I needed to the end of Troy&#8217;s proxy script.</p>
<p>So, the flow of events is:</p>
<ul>
<li>Client issues an Ajax POST request to the proxy script.</li>
<li>Proxy forwards the request to the image-generation program and converts the resultant image into a base64 data string.
<ul>
<li>For IE, the proxy saves the string to a file and returns the filename to the client.</li>
<li>For all other browsers, the proxy returns the string to the client.</li>
</ul>
</li>
<li>Client&#8217;s Ajax &#8220;on success&#8221; callback assigns the relevant string to the HTML image&#8217;s <em>src</em> attribute.</li>
</ul>
<p>Anyway, <a href="http://gingerbbm.com/demo/imagebypost/">here&#8217;s a demo page</a>.<br />
And <a href="http://gingerbbm.com/demo/imagebypost/imagebypost.zip">here&#8217;s a zip</a> containing all the necessary files.</p>
<h2>A Few Considerations</h2>
<p>The IE workaround involves creating a file on the server that contains the base64 representation of the generated image. The format of the file must be as follows in terms of line breaks, and in terms of the double quotes around the boundary parameter:</p>
<pre>Content-Type: multipart/related; boundary="_ANY_SEPARATOR"

--_ANY_SEPARATOR
Content-Location:somestring
Content-Transfer-Encoding:base64

R0lGODlh...snip...AACgAACoAAC</pre>
<p>The folder in which the MHTML files are generated obviously requires the appropriate privileges. If this method were used in a production environment an automated procedure would be required to delete periodically old MHTML files. <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" target="_blank">IE8</a> is a great improvement over previous versions and actually supports the data URI scheme, so one day in the distant future all the MHTML workaround shenanigans will be unnecessary. But until then we&#8217;re stuck with having to write a file to the server every time &#8211; and if we&#8217;re doing that it might be preferable simply to write the image to the server and return its URL to the browser. At least it would mean no JavaScript browser-sniffing, and that&#8217;s no bad thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/06/html-images-using-long-get-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bookmarklets</title>
		<link>http://gingerbbm.com/2009/05/bookmarklets/</link>
		<comments>http://gingerbbm.com/2009/05/bookmarklets/#comments</comments>
		<pubDate>Fri, 15 May 2009 23:58:15 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[bookmarklet]]></category>

		<guid isPermaLink="false">http://gingerbbm.wordpress.com/?p=87</guid>
		<description><![CDATA[Not a day goes by without the need at some point to look up a location on Google Maps (UK). Despite the obvious benefits of the quick search bars in All Modern Browsers, I&#8217;ve not really taken to them. In &#8230; <a href="http://gingerbbm.com/2009/05/bookmarklets/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Not a day goes by without the need at some point to look up a location on <a href="http://maps.google.co.uk" target="_blank">Google Maps</a> (UK). Despite the obvious benefits of the quick search bars in All Modern Browsers, I&#8217;ve not really taken to them. In theory they&#8217;re a great feature, but I find that they <a href="http://www.dontmakemethink.com/" target="_blank">make me think</a> too much. <span id="more-87"></span>Like so:</p>
<ol>
<li>pull down the list of search engines</li>
<li>scan for the one I want</li>
<li>type and go</li>
</ol>
<p>To reduce friction when searching in common places I have the following bookmarklets defined.</p>
<h3>Google Maps</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span>p<span style="color: #339933;">=</span><span style="color: #000066;">prompt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GMaps%20search:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span>20<span style="color: #009900;">&#123;</span><span style="color: #339933;">%</span>20p<span style="color: #339933;">=</span>p.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\./g</span><span style="color: #339933;">,%</span>20<span style="color: #3366CC;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>window.<span style="color: #660066;">location</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'http://maps.google.co.uk?q='</span><span style="color: #339933;">+</span>p.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Dictionary.com</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">void</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">href</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'http://dictionary.reference.com/browse/'</span><span style="color: #339933;">+</span><span style="color: #000066;">prompt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Dictionary.com%20search:'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h3>Prototype API</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span>p<span style="color: #339933;">=</span><span style="color: #000066;">prompt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Prototype%20API%20search:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>if<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span>20<span style="color: #009900;">&#123;</span><span style="color: #339933;">%</span>20p<span style="color: #339933;">=</span>p.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\./g</span><span style="color: #339933;">,%</span>20<span style="color: #3366CC;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>window.<span style="color: #660066;">location</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'http://prototypejs.org/api/'</span><span style="color: #339933;">+</span>p.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>To use these JavaScript snippets, assign them to a bookmark in your browser. For ease of access, place the bookmark on the Bookmarks Toolbar. This reduces the workflow to:</p>
<ol>
<li>click the bookmarklet</li>
<li>type and go</li>
</ol>
<p>It&#8217;s not rocket science. A simple Google search reveals all sorts of much-more-advanced bookmarklets. As far as bookmarklets for straightfoward searching are concerned, any more than three or so would feel like too many to me, and I&#8217;d opt for the browser&#8217;s quick search bar.</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/05/bookmarklets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing a Win32 DLL in C++ for Oracle extproc</title>
		<link>http://gingerbbm.com/2009/05/writing-a-win32-dll-in-c-for-oracle-extproc/</link>
		<comments>http://gingerbbm.com/2009/05/writing-a-win32-dll-in-c-for-oracle-extproc/#comments</comments>
		<pubDate>Fri, 08 May 2009 15:35:07 +0000</pubDate>
		<dc:creator>Stuart</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[extproc]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[win32 dll]]></category>

		<guid isPermaLink="false">http://gingerbbm.wordpress.com/?p=4</guid>
		<description><![CDATA[I was recently tasked with writing a Win32 DLL containing functionality to be used by Oracle procedures and functions via extproc. This post describes the steps required to achieve this using Microsoft Visual Studio 2005 and Oracle 10g. Creating the &#8230; <a href="http://gingerbbm.com/2009/05/writing-a-win32-dll-in-c-for-oracle-extproc/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was recently tasked with writing a Win32 DLL containing functionality to be used by Oracle procedures and functions via <a href="http://download.oracle.com/docs/cd/A58617_01/server.804/a58236/10_procs.htm" target="_blank">extproc</a>. This post describes the steps required to achieve this using Microsoft Visual Studio 2005 and Oracle 10g.  <span id="more-14"></span></p>
<h2><strong>Creating the DLL</strong></h2>
<p>I used Visual Studio 2005 for this but I&#8217;m sure 2003 or 2008 can be used in a similar way. The important step is to choose the appropriate project type.  Do the following:</p>
<ul>
<li>File > New > Project</li>
<li>Project type: Visual C++ > Win32</li>
<li>Template: Win32 Console Application</li>
<li>Give your project a name (I have used &#8220;rwnet64&#8243; in this example)</li>
</ul>
<p><a href="http://gingerbbm.com/wordpress/wp-content/uploads/2009/05/001-create-project.png"><img class="alignnone size-full wp-image-108" title="001-create-project" src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/05/001-create-project.png" alt="001-create-project" width="420" height="305" /></a></p>
<p>When you click OK you should see the Win32 Application Wizard:</p>
<p><a href="http://gingerbbm.com/wordpress/wp-content/uploads/2009/05/002-create-project.png"><img class="alignnone size-full wp-image-109" title="002-create-project" src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/05/002-create-project.png" alt="002-create-project" width="420" height="305" /></a></p>
<p>Click Next to configure the application settings. As shown here, ensure that the DLL radio button is selected:</p>
<p><a href="http://gingerbbm.com/wordpress/wp-content/uploads/2009/05/003-create-project.png"><img class="alignnone size-full wp-image-110" title="003-create-project" src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/05/003-create-project.png" alt="003-create-project" width="420" height="305" /></a></p>
<p>Click Finish to see the generated code:</p>
<p><a href="http://gingerbbm.com/wordpress/wp-content/uploads/2009/05/004-configuration.png"><img class="alignnone size-full wp-image-111" title="004-configuration" src="http://gingerbbm.com/wordpress/wp-content/uploads/2009/05/004-configuration.png" alt="004-configuration" width="420" height="272" /></a></p>
<p>The highlighted block shows a <a href="http://msdn.microsoft.com/en-us/library/ms682583(VS.85).aspx" target="_blank">default entry point</a> for the DLL. This can be safely deleted.</p>
<p>At this point the project should compile and we&#8217;re ready to add some code.</p>
<p>The following is an example of a simple function. The <strong>important thing to note</strong> is the compiler directive <strong>__declspec(dllexport)</strong> which is required for extproc to load the DLL successfully.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">extern</span> <span style="color: #FF0000;">&quot;C&quot;</span> __declspec<span style="color: #008000;">&#40;</span>dllexport<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">int</span> addTen <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> val<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> val <span style="color: #000040;">+</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The project should compile. Build the DLL <strong>as a Release build</strong> (a Debug build won&#8217;t work with extproc), then copy it over to your Oracle server&#8217;s BIN or LIB folder.</p>
<h2><strong>Preparing Oracle</strong></h2>
<p>I&#8217;m going to assume that your instance of Oracle is already configured to use extproc, i.e. that <em>listener.ora</em> and <em>tnsnames.ora</em> have been set up correctly. I might come back and explain how to do this later, but in the meantime just Google for it.</p>
<p>To call our DLL from a PL/SQL routine via extproc, we need:</p>
<ol>
<li>an Oracle library</li>
<li>a PL/SQL routine</li>
</ol>
<p>The Oracle library can be created like this at a SQL*Plus prompt:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #33cc33;">&gt;</span> create or replace library rwnet64
  as 'e:\oracle\product\10.2.0\db_2\lib\rwnet64.dll;
&nbsp;
Library created.</pre></div></div>

<p><strong>Note: </strong>If this fails because your user doesn&#8217;t have the necessary privileges, run the following from a SQL*Plus prompt connected as the system user (where <em>myuser </em>is, er, your user):</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #33cc33;">&gt;</span> grant create library to myuser;</pre></div></div>

<p>With the Oracle library created, we now need some code! Here&#8217;s an example package body:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> package body dlltest_pkg <span style="color: #993333; font-weight: bold;">AS</span>
&nbsp;
    <span style="color: #993333; font-weight: bold;">FUNCTION</span> addTen<span style="color: #66cc66;">&#40;</span>num <span style="color: #993333; font-weight: bold;">IN</span> pls_integer<span style="color: #66cc66;">&#41;</span>
    <span style="color: #993333; font-weight: bold;">RETURN</span> pls_integer
    <span style="color: #993333; font-weight: bold;">AS</span> external
    <span style="color: #993333; font-weight: bold;">LANGUAGE</span> c
    library rwnet64
    name <span style="color: #ff0000;">&quot;addTen&quot;</span>
    parameters <span style="color: #66cc66;">&#40;</span>num <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">END</span> dlltest_pkg;
<span style="color: #66cc66;">/</span></pre></div></div>

<p>Once you&#8217;ve created the package (and you&#8217;ll need to write the accompanying package header script) the DLL can now be tested from a SQL*Plus prompt with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #33cc33;">&gt;</span> select dlltest_pkg.addTen<span style="color: #33cc33;">(</span>5<span style="color: #33cc33;">)</span> from dual;
&nbsp;
DLLTEST_PKG.ADDTEN<span style="color: #33cc33;">(</span>5<span style="color: #33cc33;">)</span>
---------------------
 15</pre></div></div>

<p>And that proves it: calling a function in a Win32 DLL written in C++ from a function in an Oracle package via extproc.</p>
<p>If you got this far you are now at liberty to create useful functionality in your DLL. One thing to watch out for is the choice of datatypes between the DLL and PL/SQL. <a href="http://download.oracle.com/docs/cd/A58617_01/server.804/a58236/10_procs.htm#442298" target="_blank">This table is a useful starting point</a> for ironing out any issues.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbbm.com/2009/05/writing-a-win32-dll-in-c-for-oracle-extproc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

