<?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; c++</title>
	<atom:link href="http://gingerbbm.com/tag/c/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 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>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>

