<?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>Saved for later reference &#187; ctypes</title>
	<atom:link href="http://www.lejordet.com/tag/ctypes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lejordet.com</link>
	<description>online repository of stuff I had to google for hours to figure out</description>
	<lastBuildDate>Tue, 31 Jan 2012 19:37:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Simple Python ctypes Example &#8211; in Windows</title>
		<link>http://www.lejordet.com/2009/04/simple-python-ctypes-example-in-windows/</link>
		<comments>http://www.lejordet.com/2009/04/simple-python-ctypes-example-in-windows/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 08:56:11 +0000</pubDate>
		<dc:creator>Lars Erik Jordet</dc:creator>
				<category><![CDATA[compiling]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[win32]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.lejordet.com/?p=27</guid>
		<description><![CDATA[I found this blog post, Simple Python ctypes Example, on Reddit, and thought I'd see how different the procedure was for Windows. As it turns out, it was fairly similar.]]></description>
			<content:encoded><![CDATA[<p>I found this blog post, 
<a  href="http://fsckd.com/2009/4/25/simple-python-ctypes-example/" onclick="javascript:pageTracker._trackPageview('/external/fsckd.com/2009/4/25/simple-python-ctypes-example/');" >Simple Python ctypes Example</a>, on 
<a  href="http://www.reddit.com/r/programming/" onclick="javascript:pageTracker._trackPageview('/external/www.reddit.com/r/programming/');" >Reddit</a>, and thought I&#8217;d see how different the procedure was for Windows.</p>
<h2>What you&#8217;ll need:</h2>
<ul>
<li>
<a  href="http://www.microsoft.com/express/vc/" onclick="javascript:pageTracker._trackPageview('/external/www.microsoft.com/express/vc/');" >Visual C# 2008 Express Edition</a> (Visual Studio 2008 will also work &#8211; and is what I used, but will again base this on free downloads)</li>
<li>
<a  href="http://www.python.org" onclick="javascript:pageTracker._trackPageview('/external/www.python.org');" >Python</a> 2.5 or higher (I used Python 2.6.2)</li>
</ul>
<h2>Creating the DLL</h2>
<p>Create a file <tt>dlltest.c</tt> with the following content:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;windows.h&gt;</span><br />
<br />
BOOL APIENTRY DllMain<span style="color: #009900;">&#40;</span>HANDLE hModule<span style="color: #339933;">,</span> DWORD dwReason<span style="color: #339933;">,</span> LPVOID lpReserved<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> TRUE<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
__declspec<span style="color: #009900;">&#40;</span>dllexport<span style="color: #009900;">&#41;</span> <span style="color: #993333;">int</span><br />
multiply<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> num1<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> num2<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> num1 <span style="color: #339933;">*</span> num2<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Start the <strong>Visual Studio 2008 Command Prompt </strong>from the Start Menu (this sets up the path for the compiler and other files, which makes things easier) and go to the directory you saved dlltest.c to.</p>
<p>Compile it with the following command:</p>
<pre>cl -LD dlltest.c -Fetest.dll</pre>
<h2>Testing the DLL</h2>
<p>As in the original Linux example, this DLL is easily tested using similar commands:</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> ctypes <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span> libtest <span style="color: #66cc66;">=</span> cdll.<span style="color: black;">LoadLibrary</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'test.dll'</span><span style="color: black;">&#41;</span><br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">print</span> libtest.<span style="color: black;">multiply</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff4500;">4</span></div></td></tr></tbody></table></div>
<p>So, that is the simplest DLL possible, I&#8217;ll look at (same as the original blog post) applying it to more complex examples later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lejordet.com/2009/04/simple-python-ctypes-example-in-windows/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
