<?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; Oracle</title>
	<atom:link href="http://gingerbbm.com/tag/oracle-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://gingerbbm.com</link>
	<description>User Experience Design &#38; Software Development</description>
	<lastBuildDate>Wed, 16 May 2012 22:32:25 +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>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>

