<?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>Tom's Blog &#187; General .NET</title>
	<atom:link href="http://tom-shelton.net/index.php/category/net/general-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://tom-shelton.net</link>
	<description></description>
	<lastBuildDate>Mon, 04 Jan 2010 02:14:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a Managed Wrapper for a Lib File</title>
		<link>http://tom-shelton.net/index.php/2008/12/11/creating-a-managed-wrapper-for-a-lib-file/</link>
		<comments>http://tom-shelton.net/index.php/2008/12/11/creating-a-managed-wrapper-for-a-lib-file/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 19:51:20 +0000</pubDate>
		<dc:creator>Tom Shelton</dc:creator>
				<category><![CDATA[General .NET]]></category>

		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/12/11/creating-a-managed-wrapper-for-a-lib-file/</guid>
		<description><![CDATA[So, you have a lib file and it&#8217;s corresponding header file, and you need to use it from your managed application.&#160; Now what?&#160; Well, the simplest answer is to create a C++/CLR wrapper DLL to expose the functionality you need.&#160; Here&#8217;s a step-by-step walk through of the process.
&#160;




1.


The first step in this walk-through is to [...]]]></description>
			<content:encoded><![CDATA[<p>So, you have a lib file and it&#8217;s corresponding header file, and you need to use it from your managed application.&nbsp; Now what?&nbsp; Well, the simplest answer is to create a C++/CLR wrapper DLL to expose the functionality you need.&nbsp; Here&#8217;s a step-by-step walk through of the process.
<p>&nbsp;</p>
<table cellspacing="0" cellpadding="2" width="914" border="0">
<tbody>
<tr>
<td valign="top" width="31">
<p>1.</p>
</td>
<td valign="top" width="881">
<p>The first step in this walk-through is to create lib file that we want to expose in our managed code.&nbsp; To do this, create a blank Visual Studio Solution and give it a name.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/001.png"><img style="border-width:0;" height="544" alt="001" src="http://tomshelton.files.wordpress.com/2008/12/001-thumb.png" width="808" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>2.</p>
</td>
<td valign="top" width="881">
<p>Once we have a solution, lets add a C++ static library project to the solution.&nbsp; To do this, go to Solution Explorer and right click on the solution.&nbsp; On the context menu, select &#8220;Add -&gt; New Project&#8221;.&nbsp;&nbsp; This should bring up the &#8220;Add New Project&#8221; dialog.&nbsp; Add a new C++ Win32 project.&nbsp; Give it a name, and select OK.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/002.png"><img style="border-width:0;" height="516" alt="002" src="http://tomshelton.files.wordpress.com/2008/12/002-thumb.png" width="808" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>3.</p>
</td>
<td valign="top" width="881">
<p>When the &#8220;Win32 Application Wizard&#8221; comes up, select the &#8220;Next&#8221; button.&nbsp; Under Application Type, select &#8220;Static Library&#8221;.&nbsp; Also, for this example I turned off &#8220;Precompiled header&#8221;.&nbsp; And select &#8220;Finish&#8221;.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/003.png"><img style="border-width:0;" height="532" alt="003" src="http://tomshelton.files.wordpress.com/2008/12/003-thumb.png" width="629" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>4.</p>
</td>
<td valign="top" width="881">
<p>Go to the Solution Explorer again, and right click on your static library project, and select &#8220;Add -&gt; Class&#8230;&#8221;.&nbsp; This will bring up the &#8220;Add Class&#8221; dialog, select &#8220;Add&#8221;.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/005.png"><img style="border-width:0;" height="516" alt="005" src="http://tomshelton.files.wordpress.com/2008/12/005-thumb.png" width="808" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>5.</p>
</td>
<td valign="top" width="881">
<p>Fill in the information in the &#8220;Generic C++ Class Wizard&#8221;.&nbsp; Select &#8220;Finish&#8221;.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/006.png"><img style="border-width:0;" height="532" alt="006" src="http://tomshelton.files.wordpress.com/2008/12/006-thumb.png" width="629" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>6.</p>
</td>
<td valign="top" width="881">
<p>Modify the resulting AddClass.h file to look like this:</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#cc6633;">#pragma</span> once</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">namespace</span> AddTwoNumbersLib</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">class</span> AddClass</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">public</span>:</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">double</span> Add (<span style="color:#0000ff;">double</span> x, <span style="color:#0000ff;">double</span> y);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     };</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> }</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>7.</p>
</td>
<td valign="top" width="881">
<p>Let&#8217;s provide the implementation in AddClass.cpp:</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> #include <span style="color:#006080;">"AddClass.h"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">namespace</span> AddTwoNumbersLib</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">double</span> AddClass::Add(<span style="color:#0000ff;">double</span> x, <span style="color:#0000ff;">double</span> y)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         <span style="color:#0000ff;">return</span> x + y;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> }</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p>At this point, you should be able to successfully compile our example lib file.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>8.</p>
</td>
<td valign="top" width="881">
<p>OK &#8211; we are now setup for the rest of this example.&nbsp; We have a lib file and a header file that we will expose to our managed application.&nbsp; To do this, we will create a new CLR class library, using C++/CLI.&nbsp; So, go back to the Solution Explorer and right click on the solution.&nbsp; Select &#8220;Add -&gt; New Project&#8230;&#8221;.&nbsp;&nbsp; In the Project Types treeview, select &#8220;Visual C++ -&gt; CLR&#8221;.&nbsp; In the Templates pane, select &#8220;Class Library&#8221;, and give it a name and select OK.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/007.png"><img style="border-width:0;" height="516" alt="007" src="http://tomshelton.files.wordpress.com/2008/12/007-thumb.png" width="808" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>9.</p>
</td>
<td valign="top" width="881">
<p>Now we need to set a couple of project properties to be able to use the lib file.&nbsp; So, in Solution Explorer, right click on the ClrWrapper project and select Properties.&nbsp; In the &#8220;ClrWrapper Property Pages&#8221;,&nbsp; select &#8220;All Configurations&#8221; in the Configuration drop down.&nbsp; Then, select &#8220;Configuration Properties -&gt; C/C++ -&gt; General&#8221;.&nbsp; Modify the &#8220;Additional Include Directories&#8221; property, to point to the directory containing the header file for the .lib.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/008.png"><img style="border-width:0;" height="533" alt="008" src="http://tomshelton.files.wordpress.com/2008/12/008-thumb.png" width="763" border="0"></a><a href="http://tomshelton.files.wordpress.com/2008/12/002.png"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p>Selecting the browse button will bring up the &#8220;Additional Include Directories&#8221; dialog.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/009.png"><img style="border-width:0;" height="370" alt="009" src="http://tomshelton.files.wordpress.com/2008/12/009-thumb.png" width="540" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p>Clicking in the list at the top, will again give you a browse button that will bring up a folder browser dialog.&nbsp; Use the folder browser to point to the directory where your lib files header is in.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/010.png"><img style="border-width:0;" height="370" alt="010" src="http://tomshelton.files.wordpress.com/2008/12/010-thumb.png" width="524" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p>Select OK, then Apply in the &#8220;ClrWrapper Property Pages&#8221;.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>10.</p>
</td>
<td valign="top" width="881">
<p>Now, we need to tell the linker where the lib file is.&nbsp; We could do this by adding a project reference to the AddTwoNumbersLib project &#8211; but, since we are trying to simulate us only having the lib, we&#8217;ll do it the hard way!</p>
<p>With the &#8220;ClrWrapper Property Pages&#8221; dialog still open, &#8220;Configuration Properties -&gt; Linker -&gt; Input&#8221;.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/011.png"><img style="border-width:0;" height="533" alt="011" src="http://tomshelton.files.wordpress.com/2008/12/011-thumb.png" width="763" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p>Select, &#8220;Additional Dependencies&#8221; and then the browse button that appears.&nbsp; This will bring up the &#8220;Additional Dependencies&#8221; dialog.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/012.png"><img style="border-width:0;" height="376" alt="012" src="http://tomshelton.files.wordpress.com/2008/12/012-thumb.png" width="508" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p>In the list at the top, insert the full path to the lib file (make sure it&#8217;s in quotes).</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881"><a href="http://tomshelton.files.wordpress.com/2008/12/013.png"><img style="border-width:0;" height="370" alt="013" src="http://tomshelton.files.wordpress.com/2008/12/013-thumb.png" width="492" border="0"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p>Select OK.&nbsp; And OK again on the &#8220;ClrWrapper Property Pages&#8221;.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>11.</p>
</td>
<td valign="top" width="881">
<p>Now, lets create our wrapper.&nbsp; Modify the following files</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p><strong>stdafx.h</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">// stdafx.h : include file for standard system include files,</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#008000;">// or project specific include files that are used frequently,</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#008000;">// but are changed infrequently</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#cc6633;">#pragma</span> once</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> #include <span style="color:#006080;">"AddClass.h"</span></pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p><strong>ClrWrapper.h</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">// ClrWrapper.h</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#cc6633;">#pragma</span> once</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#0000ff;">using</span> <span style="color:#0000ff;">namespace</span> System;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> <span style="color:#0000ff;">namespace</span> ClrWrapper {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">ref</span> <span style="color:#0000ff;">class</span> AddClass</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     <span style="color:#0000ff;">public</span>:</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>         <span style="color:#0000ff;">double</span> Add (<span style="color:#0000ff;">double</span> x, <span style="color:#0000ff;">double</span> y);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     };</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span> }</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p><strong>ClrWrapper.cpp</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">// This is the main DLL file.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> #include <span style="color:#006080;">"stdafx.h"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> #include <span style="color:#006080;">"ClrWrapper.h"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">using</span> <span style="color:#0000ff;">namespace</span> ClrWrapper;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> <span style="color:#0000ff;">double</span> AddClass::Add(<span style="color:#0000ff;">double</span> x, <span style="color:#0000ff;">double</span> y)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     <span style="color:#0000ff;">return</span> AddTwoNumbersLib::AddClass::Add (x, y);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> }</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p>With that done, you should be able to compile the solution.</p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="31">
<p>12.</p>
</td>
<td valign="top" width="881">
<p>Now, create a managed project in the language of your choice &#8211; C# or VB, and add a reference to your the ClrWrapper.dll.&nbsp; Once done, you should be able to write, compile, and run code like the following:</p>
<p><a href="http://tomshelton.files.wordpress.com/2008/12/008.png"></a></td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p><strong>C#</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">using</span> System;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">using</span> ClrWrapper;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">namespace</span> CSharpClient</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     <span style="color:#0000ff;">class</span> Program</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main ( <span style="color:#0000ff;">string</span>[] args )</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>             AddClass addClass = <span style="color:#0000ff;">new</span> AddClass ();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>             Console.WriteLine ( addClass.Add ( 1, 2 ) );</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>             Console.WriteLine ( addClass.Add ( 2, 2 ) );</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>             Console.WriteLine ( addClass.Add ( 649, 1 ) );</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span> }</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<p><strong>VB</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="31">&nbsp;</td>
<td valign="top" width="881">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">Option</span> Explicit <span style="color:#0000ff;">On</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">Option</span> Strict <span style="color:#0000ff;">On</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">Imports</span> System</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#0000ff;">Imports</span> ClrWrapper</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> <span style="color:#0000ff;">Module</span> Program</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     <span style="color:#0000ff;">Sub</span> Main()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         <span style="color:#0000ff;">Dim</span> a <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">New</span> AddClass()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>         Console.WriteLine(a.Add(1, 2))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>         Console.WriteLine(a.Add(2, 2))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>         Console.WriteLine(a.Add(649, 1))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     <span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>  </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span> <span style="color:#0000ff;">End</span> Module</pre>
</div>
</div>
</td>
</tr>
</tbody>
</table>
<td width="702" valign="top">
<p>Hopefully, someone will find this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://tom-shelton.net/index.php/2008/12/11/creating-a-managed-wrapper-for-a-lib-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A Simple Method for Evaluating Mathematical Expressions at Runtime</title>
		<link>http://tom-shelton.net/index.php/2008/12/02/a-simple-method-for-evaluating-mathematical-expressions-at-runtime/</link>
		<comments>http://tom-shelton.net/index.php/2008/12/02/a-simple-method-for-evaluating-mathematical-expressions-at-runtime/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 06:31:54 +0000</pubDate>
		<dc:creator>Tom Shelton</dc:creator>
				<category><![CDATA[General .NET]]></category>

		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/12/02/a-simple-method-for-evaluating-mathematical-expressions-at-runtime/</guid>
		<description><![CDATA[
Over the years, on the forums I frequent, the question has come up from time-to-time on how to dynamically evaluate expressions built at runtime.&#160; Often, the suggestions range from dynamic code compilation to creating expression evaluators.&#160; While these suggestions are not necessarily bad &#8211; they do have some drawbacks.
The main problem with dynamic code compilation, [...]]]></description>
			<content:encoded><![CDATA[</p>
<p>Over the years, on the forums I frequent, the question has come up from time-to-time on how to dynamically evaluate expressions built at runtime.&nbsp; Often, the suggestions range from dynamic code compilation to creating expression evaluators.&nbsp; While these suggestions are not necessarily bad &#8211; they do have some drawbacks.</p>
<p>The main problem with dynamic code compilation, is that it is slow.&nbsp; This may not be an issue if you are only going to do one or two calculations &#8211; but, if you need to perform your calculations hundreds or thousands of times, then the performance hit can become significant.&nbsp; For example,&nbsp; on the MSDN Visual C# forum, I was recently involved in a <a href="http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/5d5d7fba-fb96-4328-97b8-2a2164dfdc14" target="_blank">thread</a> where the original poster was using dynamic compilation to evaluate expressions thousands of times and was looking for ways to speed up the process.&nbsp; I suggested he try the method I am about to present here, and his processing time dropped from the approximately 7 hours to 49 seconds.&nbsp; Not bad.</p>
<p>The other method, creating an expression evaluator, suffers from complexity.&nbsp; It isn&#8217;t an easy task.&nbsp; Of course, there are enough examples and pre-built libraries to perform this task that no one should have to write such a beast anymore (except maybe as an exercise).&nbsp; But, why hunt down a third party library, when Microsoft has given us a built in expression evaluator?&nbsp;&nbsp;&nbsp; Well, sort of.&nbsp; </p>
<p>The expression evaluation mechanism that I am referring to is the JScript Eval method.&nbsp; It seems that many do not realize that JScript is an official .NET language.&nbsp; I suspect that is because the IDE has no built in support for JScript.NET.&nbsp;&nbsp; Here is a method for creating a simple wrapper library to expose the JScript Eval method to your C# or VB program.</p>
<p>&nbsp;</p>
<table cellspacing="0" cellpadding="5" width="914" border="0">
<tbody>
<tr>
<td valign="top" width="19">
<p>1.</p>
</td>
<td valign="top" width="894">
<p>Since there is no IDE support for the JScript.NET language, create and save the Evaluator.js file in the external text editor of your choice.&nbsp; </p>
</td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td align="middle" width="894">
<p><a href="http://tomshelton.files.wordpress.com/2008/12/image01.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="494" alt="image01" src="http://tomshelton.files.wordpress.com/2008/12/image01-thumb.png" width="681" border="0"></a> </p>
</td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td valign="top" width="894">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="19">
<p>2.</p>
</td>
<td valign="top" width="894">
<p>Use the &#8220;Visual Studio 2008 Command Prompt&#8221; (Start -&gt; Programs -&gt; Microsoft Visual Studio 2008 -&gt; Visual Studio Tools) to navigate to the directory that contains the Evaluator.js file.&nbsp; Once there, compile the source, like this:</p>
</td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td align="middle" width="894"><a href="http://tomshelton.files.wordpress.com/2008/12/image02.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="337" alt="image02" src="http://tomshelton.files.wordpress.com/2008/12/image02-thumb.png" width="665" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td valign="top" width="894">
<p>Ok, so I didn&#8217;t use the VS command prompt <img src='http://tom-shelton.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &nbsp; I used the Windows PowerShell console.&nbsp; I translated the bat file that the VS command prompt uses and put it in my $PROFILE. </p>
</td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td valign="top" width="894">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="19">
<p>3.</p>
</td>
<td valign="top" width="894">
<p>Now, create your Visual Basic or C# project.&nbsp; Right click on your project in the Solution Explorer, and select &#8220;Add Reference&#8230;&#8221;.&nbsp; On the .NET tab select Microsoft.JScript:</p>
</td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td align="middle" width="894"><a href="http://tomshelton.files.wordpress.com/2008/12/image03.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="507" alt="image03" src="http://tomshelton.files.wordpress.com/2008/12/image03-thumb.png" width="596" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td valign="top" width="894">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="19">
<p>4.</p>
</td>
<td valign="top" width="894">
<p>Again, Right click on your project in the Solution Explorer, and select &#8220;Add Reference&#8230;&#8221;.&nbsp;&nbsp; Select the &#8220;Browse&#8221; tab, and find and select the Evaluator.dll that we just compiled:</p>
</td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td align="middle" width="894"><a href="http://tomshelton.files.wordpress.com/2008/12/image04.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="507" alt="image04" src="http://tomshelton.files.wordpress.com/2008/12/image04-thumb.png" width="596" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="19">&nbsp;</td>
<td valign="top" width="894">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="19">
<p>5.</p>
</td>
<td valign="top" width="894">
<p>Let&#8217;s write some code to use the dll.</p>
<h6><strong><u>C#</u></strong></h6>
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">using</span> System;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">namespace</span> CSharpExample</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">class</span> Program</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main ( <span style="color:#0000ff;">string</span>[] args )</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">string</span> expression = <span style="color:#0000ff;">string</span>.Format ( <span style="color:#006080;">"(Math.pow({0}, {1}) * {2}) / {3}"</span>, 50, 2, 3.5, 16.25 );</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New
', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">            Console.WriteLine ( Calculate ( expression ) );</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">decimal</span> Calculate ( <span style="color:#0000ff;">string</span> expression )</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">return</span> Convert.ToDecimal ( <span style="color:#0000ff;">new</span> Evaluator ().Evaluate ( expression ) );</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">}</pre>
</div>
</div>
<h6><u>VB</u></h6>
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">Option</span> Strict <span style="color:#0000ff;">On</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">Option</span> Explicit <span style="color:#0000ff;">On</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">Module</span> VBExample</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">Sub</span> Main()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">Dim</span> expression <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">String</span> = <span style="color:#0000ff;">String</span>.Format(<span style="color:#006080;">"(Math.pow({0}, {1}) * {2}) / {3}"</span>, 50, 2, 3.5, 16.25)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        Console.WriteLine(Calculate(expression))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&nbsp;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">Function</span> Calculate(<span style="color:#0000ff;">ByVal</span> expression <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">String</span>) <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">Decimal</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">Return</span> Convert.ToDecimal(<span style="color:#0000ff;">New</span> Evaluator().Evaluate(expression))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Function</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Module</span></pre>
</div>
</div>
</td>
</tr>
</tbody>
</table>
<p>This example, is pretty simplistic &#8211; for example, the Calculate methods do not do any sort of checking to make sure that the expression passed is a valid mathematical expression.&nbsp; I used the System.String.Format method to illustrate one way of getting values into an expression to be evaluated.&nbsp; It should be noted that this method will not only evaluate mathematical expressions, but arbitrary JScript code.&nbsp; As long as you are using a version of .NET &gt;= 1.1, then this shouldn&#8217;t be much of an issue because unless you pass the string &#8220;unsafe&#8221; to the optional second parameter of the JScript Eval method, then the code will run in a limited security context.&nbsp;&nbsp; If you decide to ever use the Eval method with the &#8220;unsafe&#8221; option, then you will want to make sure that all of your strings are obtained from a trusted source.</p>
]]></content:encoded>
			<wfw:commentRss>http://tom-shelton.net/index.php/2008/12/02/a-simple-method-for-evaluating-mathematical-expressions-at-runtime/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Calling Managed Code from a DLL Created in Visual C++ 2008</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/</link>
		<comments>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 03:57:20 +0000</pubDate>
		<dc:creator>Tom Shelton</dc:creator>
				<category><![CDATA[General .NET]]></category>

		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/</guid>
		<description><![CDATA[I got involved in a discussion the other day over on the MSDN Visual C# General forum, in which a poster had a situation where he needed to create a dll in C++ to extend a 3rd party application &#8211; but, he wanted to reuse some of his C# code from that dll.&#160; So, I [...]]]></description>
			<content:encoded><![CDATA[<p>I got involved in a discussion the other day over on the MSDN Visual C# General forum, in which a poster had a situation where he needed to create a dll in C++ to extend a 3rd party application &#8211; but, he wanted to reuse some of his C# code from that dll.&nbsp; So, I am putting this post together to discuss the steps I took to get this working in Visual C++ 2008.&nbsp; The goal of this article is to provide a step-by-step walk through.&nbsp; If you follow all the steps (and I am communicating coherently) &#8211; then you should have a working code sample at the end of this article.</p>
<p>Since in real life, I&#8217;m primarily a C# developer, the order I do things in might be a little different then say someone who is primarily a VB.NET developer or a C++ developer.&nbsp; Don&#8217;t let that throw you,&nbsp; if things are in a different order,&nbsp; just search around until you find the option.</p>
<p>&nbsp;</p>
<table cellspacing="0" cellpadding="5" width="914" border="0">
<tbody>
<tr>
<td valign="top" width="33">
<p>1.</p>
</td>
<td valign="top" width="877">
<p>The first step is to create a new Visual C++ solution.&nbsp; Click on &#8220;File -&gt; New -&gt; Project&#8230;&#8221;.&nbsp; This brings up the &#8220;New Project&#8221; dialog.&nbsp; In the &#8220;Project Types&#8221; pane, select &#8220;Other Languages -&gt; Visual C++ -&gt; Win32&#8243;.&nbsp; Then from the &#8220;Templates&#8221; pane on the right, select &#8220;Win32 Console Application&#8221;.&nbsp; Give the project a name (I called mine CallCSharpCodeSample) and select the &#8220;OK&#8221; button.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877">
<p><a href="http://tomshelton.files.wordpress.com/2008/11/step1.png"><img style="border-width:0;" height="616" alt="step1" src="http://tomshelton.files.wordpress.com/2008/11/step1-thumb.png" width="612" border="0"></a> </p>
</td>
</tr>
<tr>
<td valign="top" width="33">
<p>&nbsp;</p>
</td>
<td valign="top" width="877">
<p>When the &#8220;Application Settings Wizard&#8221; dialog appears, select the &#8220;Finish&#8221; button. The Console project just created will become the host application to test out the dll.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">
<p>2.</p>
</td>
<td valign="top" width="877">
<p>Add a C# Class Library project.&nbsp; This library will represent the existing .NET assembly that needs to exposed via the C++ dll.&nbsp; Go to Solution Explorer, and right click on the solution and select &#8220;Add -&gt; New Project&#8230;&#8221;.&nbsp; In the &#8220;Project types&#8221; pane of the &#8220;Add New Project&#8221; dialog,&nbsp; select &#8220;Visual C# -&gt; Windows&#8221;.&nbsp; In the &#8220;Templates&#8221; pane select &#8220;Class Library&#8221;.&nbsp; Give the library a name, and then select the &#8220;OK&#8221; button.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877">
<p><a href="http://tomshelton.files.wordpress.com/2008/11/step4.png"><img style="border-width:0;" height="469" alt="step4" src="http://tomshelton.files.wordpress.com/2008/11/step4-thumb.png" width="544" border="0"></a> </p>
</td>
</tr>
<tr>
<td valign="top" width="33">
<p>3.</p>
</td>
<td valign="top" width="877">
<p>Rename the default Class1.cs to AddClass.cs and allow the IDE to rename your class.&nbsp; Insert a method to add two integers.&nbsp; Your code should look something like this:</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">using</span> System;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">using</span> System.Collections.Generic;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">using</span> System.Linq;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">using</span> System.Text;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">namespace</span> CustomCSharpLib</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AddClass</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> AddTwoNumbers ( <span style="color:#0000ff;">int</span> x, <span style="color:#0000ff;">int</span> y )</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>             <span style="color:#0000ff;">return</span> x + y;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span> }</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="33">
<p>4.</p>
</td>
<td valign="top" width="877">
<p>Setup is now complete and you&#8217;re ready to create the dll.&nbsp; Right click on the solution in Solution Explorer, select &#8220;Add -&gt; New Project&#8230;&#8221;.&nbsp; Add a new C++ Win32 project from the &#8220;Add New Project&#8221; dialog.&nbsp; And give it a name and select the &#8220;OK&#8221; button.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step7.png"><img style="border-width:0;" height="569" alt="step7" src="http://tomshelton.files.wordpress.com/2008/11/step7-thumb.png" width="576" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">
<p>5.</p>
</td>
<td valign="top" width="877">
<p>You will again be presented with the &#8220;Application Settings Wizard&#8221;.&nbsp; Select the &#8220;Next&#8221; button and set the project options as shown below.&nbsp; Select the &#8220;Finish&#8221; button.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step8b.png"><img style="border-width:0;" height="528" alt="step8b" src="http://tomshelton.files.wordpress.com/2008/11/step8b-thumb.png" width="619" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">
<p>6.</p>
</td>
<td valign="top" width="877">
<p>When the MyDll project is created, Visual Studio adds some unneeded code to &#8220;MyDll.h&#8221; and &#8220;MyDll.cpp&#8221;.&nbsp; Edit these files to look like the code below.</p>
<p><strong>MyDll.h</strong>:</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">// The following ifdef block is the standard way of creating macros which make exporting </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#008000;">// from a DLL simpler. All files within this DLL are compiled with the MYDLL_EXPORTS</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#008000;">// symbol defined on the command line. this symbol should not be defined on any project</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#008000;">// that uses this DLL. This way any other project whose source files include this file see </span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#008000;">// MYDLL_API functions as being imported from a DLL, whereas this DLL sees symbols</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#008000;">// defined with this macro as being exported.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> #ifdef MYDLL_EXPORTS</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> <span style="color:#cc6633;">#define</span> MYDLL_API __declspec(dllexport)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> <span style="color:#cc6633;">#else</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> <span style="color:#cc6633;">#define</span> MYDLL_API __declspec(dllimport)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> <span style="color:#cc6633;">#endif</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> MYDLL_API <span style="color:#0000ff;">int</span> AddTwoNumbers(<span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span>);</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<p><strong>MyDll.cpp</strong>:</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">// MyDll.cpp : Defines the exported functions for the DLL application.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#008000;">//</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> #include <span style="color:#006080;">"stdafx.h"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> #include <span style="color:#006080;">"MyDll.h"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> <span style="color:#0000ff;">using</span> <span style="color:#0000ff;">namespace</span> CustomCSharpLib;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> MYDLL_API <span style="color:#0000ff;">int</span> AddTwoNumbers(<span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span> x, <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span> y)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     <span style="color:#0000ff;">return</span> x + y;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span> }</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<p>At this point, everything should compile just fine.&nbsp; I have purposely not changed any project settings yet to enable managed code &#8211; but don&#8217;t worry, we&#8217;ll get to that!&nbsp; Let&#8217;s make sure we can actually use and call this dll from the test program first.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">
<p>7.</p>
</td>
<td valign="top" width="877">
<p>Change the default calling convention for the MyDll project to __stdcall.&nbsp; You do this by right clicking on the project in the Solution Explorer and selecting &#8220;Properties&#8230;&#8221;.&nbsp; Change the &#8220;Configuration&#8221; option to &#8220;All Configurations&#8221; and then select &#8220;C/C++ -&gt; Advanced&#8221;.&nbsp; Change the &#8220;Calling Convention&#8221; entry in the property page to &#8220;__stdcall (/Gz)&#8221; and select OK.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step10.png"><img style="border-width:0;" height="525" alt="step10" src="http://tomshelton.files.wordpress.com/2008/11/step10-thumb.png" width="753" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<p>I&#8217;m not sure this step is actually necessary, but __stdcall is the same calling convention used by the Windows API and will make your dll easily callable from languages such as Visual Basic 6 that can&#8217;t handle the default __cdecl calling convention.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">
<p>8.</p>
</td>
<td valign="top" width="877">
<p>Add a module definition file to the MyDll project &#8211; this will allow us to override the default name mangling done by the compiler, and make the dll more easily callable.&nbsp; Right click on the MyDll project in the Solution Explorer and select &#8220;Add -&gt; New Item&#8230;&#8221;.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">
<p>9.</p>
</td>
<td valign="top" width="877">
<p>In the &#8220;Templates&#8221; pane of the &#8220;Add New Item&#8221; dialog, select &#8220;Module-Definition File (.def)&#8221;.&nbsp; Give it a name.&nbsp; Select the &#8220;Add&#8221; button.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step12.png"><img style="border-width:0;" height="563" alt="step12" src="http://tomshelton.files.wordpress.com/2008/11/step12-thumb.png" width="701" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">
<p>10.</p>
</td>
<td valign="top" width="877">
<p>Edit your module definition file so that it looks like this (it will have a .def extension):</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<div class="csharpcode">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> LIBRARY    "MyDll"</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> EXPORTS</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     AddTwoNumbers @1</pre>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="33">
<p>11.</p>
</td>
<td valign="top" width="877">
<p>Edit your CallCSharpCodeSample.cpp file to look like this:</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<div class="csharpcode">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">// CallCSharpCodeSample.cpp : Defines the entry point for the console application.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#008000;">//</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> #include <span style="color:#006080;">"stdafx.h"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> #include &lt;iostream&gt;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> #include &lt;windows.h&gt;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> <span style="color:#0000ff;">using</span> <span style="color:#0000ff;">namespace</span> std;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> <span style="color:#008000;">// our function pointer definition</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> typedef <span style="color:#0000ff;">int</span> (WINAPI* LPFN_ADDFUNC)(<span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span>, <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> <span style="color:#0000ff;">int</span> _tmain(<span style="color:#0000ff;">int</span> argc, _TCHAR* argv[])</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     HMODULE hMod = LoadLibrary(_T(<span style="color:#006080;">"MyDll.dll"</span>));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>     <span style="color:#0000ff;">if</span> (hMod != NULL)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>         LPFN_ADDFUNC addFunc = (LPFN_ADDFUNC)GetProcAddress(hMod, <span style="color:#006080;">"AddTwoNumbers"</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>         <span style="color:#0000ff;">if</span> (addFunc != NULL)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>             cout &lt;&lt; addFunc(1, 2) &lt;&lt; endl;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>             cout &lt;&lt; addFunc(5, 6) &lt;&lt; endl;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>             cout &lt;&lt; addFunc(100, 10024) &lt;&lt; endl;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:n
one;margin:0;padding:0;"><span style="color:#606060;">  27:</span>         <span style="color:#0000ff;">else</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  29:</span>             cout &lt;&lt; <span style="color:#006080;">"Failed to load function AddTwoNumbers"</span> &lt;&lt; endl;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  30:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  31:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  32:</span>     <span style="color:#0000ff;">else</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  33:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  34:</span>         cout &lt;&lt; <span style="color:#006080;">"Failed to load module MyDll.dll"</span> &lt;&lt; endl;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  35:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  36:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  37:</span>     <span style="color:#0000ff;">return</span> 0;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  38:</span> }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  39:</span>&nbsp; </pre>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<p>At this point, assuming you&#8217;ve done everything correctly, you should be able to compile and run the application:</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step14.png"><img style="border-width:0;" height="342" alt="step14" src="http://tomshelton.files.wordpress.com/2008/11/step14-thumb.png" width="673" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">
<p>12.</p>
</td>
<td valign="top" width="877">
<p>Now that the dll and it&#8217;s test harness are in place, let&#8217;s return our attention to the MyDll project.&nbsp; At this step, we are going to enable CLR support.&nbsp; Right click on the MyDll project in Solution Explorer and select &#8220;Properties&#8230;&#8221;.&nbsp; Set the &#8220;Configuration&#8221; option to &#8220;All Configurations&#8221;.&nbsp; Select &#8220;Configuration Properties -&gt; General&#8221; in the menu tree, and then change the &#8220;Common Language Runtime support&#8221; option in the properties pane to &#8220;Common Language Runtime Support (/clr)&#8221; and select the &#8220;OK&#8221; button.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step15.png"><img style="border-width:0;" height="525" alt="step15" src="http://tomshelton.files.wordpress.com/2008/11/step15-thumb.png" width="753" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">
<p>13.</p>
</td>
<td valign="top" width="877">
<p>Right click on your MyDll project in the Solution Explorer and select &#8220;References&#8230;&#8221;.&nbsp; When the &#8220;Property Pages&#8221; dialog appears, select the &#8220;Add New Reference&#8230;&#8221; button.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step18a.png"><img style="border-width:0;" height="525" alt="step18a" src="http://tomshelton.files.wordpress.com/2008/11/step18a-thumb.png" width="753" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<p>In the &#8220;Add Reference&#8221; dialog, select the &#8220;Projects&#8221; tab.&nbsp; Select the CustomCSharpLib project from the list.&nbsp; Select OK.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step18b.png"><img style="border-width:0;" height="394" alt="step18b" src="http://tomshelton.files.wordpress.com/2008/11/step18b-thumb.png" width="471" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<p>This will add the reference to the C# class library to your MyDll project.&nbsp; Select OK on the &#8220;Property Pages&#8221; dialog.</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step18c.png"><img style="border-width:0;" height="525" alt="step18c" src="http://tomshelton.files.wordpress.com/2008/11/step18c-thumb.png" width="753" border="0"></a> </td>
</tr>
<tr>
<td valign="top" width="33">
<p>14.</p>
</td>
<td valign="top" width="877">
<p>Edit MyDll.cpp to look like this:</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">// MyDll.cpp : Defines the exported functions for the DLL application.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#008000;">//</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> #include <span style="color:#006080;">"stdafx.h"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> #include <span style="color:#006080;">"MyDll.h"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> <span style="color:#0000ff;">using</span> <span style="color:#0000ff;">namespace</span> CustomCSharpLib;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> MYDLL_API <span style="color:#0000ff;">int</span> AddTwoNumbers(<span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span> x, <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span> y)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     AddClass addClass;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     <span style="color:#0000ff;">return</span> addClass.AddTwoNumbers(x, y);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> }</pre>
</div>
</div>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td valign="top" width="877">
<p>Then compile and run your project.&nbsp; If all goes well, this is what you should see:</p>
</td>
</tr>
<tr>
<td valign="top" width="33">&nbsp;</td>
<td align="middle" width="877"><a href="http://tomshelton.files.wordpress.com/2008/11/step141.png"><img style="border-width:0;" height="342" alt="step14" src="http://tomshelton.files.wordpress.com/2008/11/step14-thumb1.png" width="673" border="0"></a> </td>
</tr>
</tbody>
</table>
<p>One thing I did learn from this, is if you are going to include any .NET related header files (such as vcclr.h) &#8211; don&#8217;t do it in &#8220;stdafx.h&#8221;.&nbsp; Otherwise, this file will be included in dllmain.cpp, and you will suddenly start getting an error about needing to add /clr switch to the command line.&nbsp; The reason is that DllMain can&#8217;t be a managed function &#8211; so the IDE helpfully turns off /clr for dllmain.cpp.&nbsp; There is a work around for this, you can turn on the /crl option for this file in it&#8217;s properties, but then you need to surround DllMain with some preprocessor directives to prevent it from being compiled as managed code.&nbsp; It seems easier to just include any of the .NET related headers in your MyDll.h and be done with it.</p>
<p>Well, I hope this helps someone.&nbsp; If not, it was definitely a good learning experience for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
