<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.1.2" -->
<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/"
	>

<channel>
	<title>Moy Blog</title>
	<link>http://www.moythreads.com/wordpress</link>
	<description>Abandon All Hope, Ye Who Read This Blog</description>
	<pubDate>Fri, 01 Aug 2008 02:45:41 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>
	<language>en</language>
			<item>
		<title>I&#8217;ll be speaking in Phoenix at Astricon</title>
		<link>http://www.moythreads.com/wordpress/2008/07/31/ill-be-speaking-in-phoenix-at-astricon/</link>
		<comments>http://www.moythreads.com/wordpress/2008/07/31/ill-be-speaking-in-phoenix-at-astricon/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 19:48:17 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[asterisk]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/07/31/ill-be-speaking-in-phoenix-at-astricon/</guid>
		<description><![CDATA[So, not quite, Astricon will be in Glendale AZ, 9 miles northwest from Phoenix downtown. The conference will be from September 23 - 25 and I&#8217;ll be speaking about the support for MFC/R2 I am adding to Asterisk, hope to see you there!

]]></description>
			<content:encoded><![CDATA[<p>So, not quite, <a href="http://www.astricon.net/" target="_blank">Astricon</a> will be in <a href="http://en.wikipedia.org/wiki/Glendale,_Arizona" target="_blank">Glendale AZ</a>, 9 miles northwest from Phoenix downtown. The conference will be from September 23 - 25 and I&#8217;ll be speaking about the support for <a href="http://en.wikipedia.org/wiki/R2_signalling" target="_blank">MFC/R2</a> I am adding to <a href="http://www.asterisk.org/" target="_blank">Asterisk</a>, hope to see you there!</p>
<p><a href="http://www.astricon.net/" target="_blank"><img src="http://www.moythreads.com/astricon.gif" align="center"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/07/31/ill-be-speaking-in-phoenix-at-astricon/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MFC/R2 branches for Asterisk 1.2 and 1.4</title>
		<link>http://www.moythreads.com/wordpress/2008/07/05/mfcr2-branches-for-asterisk-12-and-14/</link>
		<comments>http://www.moythreads.com/wordpress/2008/07/05/mfcr2-branches-for-asterisk-12-and-14/#comments</comments>
		<pubDate>Sat, 05 Jul 2008 05:58:06 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[asterisk]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/07/05/mfcr2-branches-for-asterisk-12-and-14/</guid>
		<description><![CDATA[I just created 2 branches for MFC/R2 support in chan_zap for Asterisk 1.2 and 1.4, this branches replaced the patches and .tar.gz I previously published, is much easier to keep them up-to-date this way. If you still need the patches you can easily get them using svn diff.
Let&#8217;s say you need a patch against Asterisk [...]]]></description>
			<content:encoded><![CDATA[<p>I just created 2 branches for MFC/R2 support in chan_zap for Asterisk 1.2 and 1.4, this branches replaced the patches and .tar.gz I previously published, is much easier to keep them up-to-date this way. If you still need the patches you can easily get them using svn diff.</p>
<p>Let&#8217;s say you need a patch against Asterisk 1.4.21.1</p>
<pre>
svn diff  http://svn.digium.com/svn/asterisk/tags/1.4.21.1 
http://svn.digium.com/svn/asterisk/team/moy/mfcr2-1.4 > asterisk-openr2-1.4.21.1.patch
</pre>
<p>That will create a patch to transform Asterisk 1.4.21.1 version into an OpenR2-enabled version. However this has other side-effect, changes that may have nothing to do with openr2 can be introduced. I therefore recommend using my branches directly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/07/05/mfcr2-branches-for-asterisk-12-and-14/feed/</wfw:commentRss>
		</item>
		<item>
		<title>OpenR2 back-ported to Asterisk 1.4 and 1.2</title>
		<link>http://www.moythreads.com/wordpress/2008/06/19/openr2-back-ported-to-asterisk-14-and-12/</link>
		<comments>http://www.moythreads.com/wordpress/2008/06/19/openr2-back-ported-to-asterisk-14-and-12/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 05:38:20 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[asterisk]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/06/19/openr2-back-ported-to-asterisk-14-and-12/</guid>
		<description><![CDATA[I just back-ported the support for MFC/R2 signaling using the OpenR2 library to Asterisk 1.4 and 1.2, go and get it while is still hot!
http://www.libopenr2.org/ 
I also updated the site to use TextMotion, a new web publishing software (aka CMS), works pretty well, kudos to the TextMotion devs.
]]></description>
			<content:encoded><![CDATA[<p>I just back-ported the support for MFC/R2 signaling using the OpenR2 library to Asterisk 1.4 and 1.2, go and get it while is still hot!</p>
<p><a href="http://www.libopenr2.org/" target="_blank">http://www.libopenr2.org/</a> </p>
<p>I also updated the site to use <a href="http://textmotion.org/" target="_blank">TextMotion</a>, a new web publishing software (aka CMS), works pretty well, kudos to the TextMotion devs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/06/19/openr2-back-ported-to-asterisk-14-and-12/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Tale of Two Bugs</title>
		<link>http://www.moythreads.com/wordpress/2008/05/25/a-tale-of-two-bugs/</link>
		<comments>http://www.moythreads.com/wordpress/2008/05/25/a-tale-of-two-bugs/#comments</comments>
		<pubDate>Sun, 25 May 2008 05:05:11 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/05/25/a-tale-of-two-bugs/</guid>
		<description><![CDATA[It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the age of bug-hunting!
Recently I fixed 2 bugs, yeah, I know I spent a lot of time fixing bugs but this 2 were quite interesting to me, not because [...]]]></description>
			<content:encoded><![CDATA[<p>It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the age of bug-hunting!</p>
<p>Recently I fixed 2 bugs, yeah, I know I spent a lot of time fixing bugs but this 2 were quite interesting to me, not because of the bugs itself, but rather because of some stuff I learned in the process like the implementation of variadic functions and how the C++ compiler optimizes certain stuff unveiling odd bugs. </p>
<blockquote><p>Bug 1</p></blockquote>
<p>Let&#8217;s analyze the first one, it was a bug I had with some Unicall R2 installation in 64 bits. The problem was simple, as soon as I loaded chan_unicall.so Asterisk crashed <img src='http://www.moythreads.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
After running Asterisk with gdb I found the crash happened inside libc function strlen that was being called by uc_log(), the Unicall logging function. As most logging C functions, uc_log is a <a href="http://en.wikipedia.org/wiki/Varargs" target="_blank">variadic function</a>. uc_log does not do any complicated stuff, is mostly just a wrapper to vsnprintf and the variable arguments were just passed on to vsnprintf and there the crash was occurring, so, how can one see the arguments a variadic function receives using gdb? First, one must know how variadic functions are implemented by the compiler and platform you are working on.</p>
<p>Most common implementation of variadic functions in C is just to define va_list as an unsigned char* pointing to the last argument of the function and each call to va_arg() retrieves the next chunk of memory of the specified size and increment va_list to point to the start of the next argument, therefore, displaying arguments is just matter of printing the memory area after the last argument. However, AMD64 has a different implementation, va_list is an array of 1 structure with members:</p>
<p>.gp_offset<br />
.fp_offset<br />
.overflow_arg_area<br />
.reg_save_area</p>
<p>gp_offset is how many bytes after reg_save_area the first argument is. To print the first variable argument that we know is an &#8220;int&#8221; we do:</p>
<p>(gdb) p *(int *)(((char *)arg_ptr[0].reg_save_area)+arg_ptr[0].gp_offset)</p>
<p>however, gp_offset will be only incremented after calling va_arg() macro, if you want to see more arguments you must increment reg_save_area by the number of bytes you know arguments take, in the case of uc_log, initial value of gp_offset is 24, probably because it receive 3 fixed arguments (8 bytes * 3). So, the first variable argument starts at .reg_save_area + 24, the second at .reg_save_area + 32 (we&#8217;re in a 64 bit machine).</p>
<p>So, what about .fp_offset and .overflow_arg_area?, well, it seems .reg_save_area is quite limited (possibly limited by the number of the processor registers) and you can never go beyond .gp_offset == 40, therefore that will only work for up to 6 arguments (including the fixed ones). .overflow_arg_area is used for any subsequent argument and .fp_offset is the pointer to the next argument on that memory area. Well, that&#8217;s enough, let&#8217;s get straight to the point, the crash was caused because unicall.h include the following prototype:</p>
<p>extern const char *uc_statet2str(int state);</p>
<p>That function returned the value passed to uc_log(&#8230;., uc_state2str()) &#8230; so what&#8217;s the issue? well, read once again the prototype and how uc_log used it. Is not a typo here in my blog, the prototype really is uc_statet2str, and the function call is uc_state2str, indeed there is a typo in the header file causing the compiler to default to the return value &#8220;int&#8221; and not const char* when compiling libmfcr2, for 64 bit platform there is 4 bytes of difference between char* and int causing a crash due to invalid memory read.</p>
<blockquote><p>Bug 2</p></blockquote>
<p>This one is easier to explain with a chunk of code, can you tell what&#8217;s wrong with it and what possible outputs will have when running it as &#8220;./test t&#8221;?</p>
<style type="text/css">
.comment { color: #999999; font-style: italic; }
.pre { color: #000099; }
.string { color: #009900; }
.char { color: #009900; }
.float { color: #996600; }
.int { color: #999900; }
.bool { color: #000000; font-weight: bold; }
.type { color: #FF6633; }
.flow { color: #FF0000; }
.keyword { color: #990000; }
.operator { color: #663300; font-weight: bold; }
</style>
<p></head><br />
<body>
<pre><span class="pre">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define SIZE 100
</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">(</span><span class="type">int</span> argc<span class="operator">,</span><span class="type"> char</span><span class="operator"> *</span>argv<span class="operator">[])
{</span><span class="type">

        char</span><span class="operator"> *</span>bufptr<span class="operator"> =</span> NULL<span class="operator">;</span><span class="flow">
        if</span><span class="operator"> (</span>argc<span class="operator"> ==</span><span class="int"> 2</span><span class="operator">) {</span><span class="type">

                char</span> inblock_buff<span class="operator">[</span>SIZE<span class="operator">];</span>
                bufptr<span class="operator"> =</span> inblock_buff<span class="operator">;</span>
                strcpy<span class="operator">(</span>bufptr<span class="operator">,</span><span class="string"> &#8220;some buffer&#8221;</span><span class="operator">);
        }</span>

        printf<span class="operator">(</span><span class="string">&#8220;buffer: %sn&#8221;</span><span class="operator">,</span> bufptr<span class="operator">);</span><span class="flow">
        if</span><span class="operator"> (</span>argc<span class="operator"> ==</span><span class="int"> 2</span><span class="operator"> &amp;&amp;</span> argv<span class="operator">[</span><span class="int">1</span><span class="operator">][</span><span class="int">0</span><span class="operator">] ==</span><span class="char"> &#8216;t&#8217;</span><span class="operator">) {</span><span class="type">

                char</span> otherbuff<span class="operator">[</span>SIZE<span class="operator">];</span>
                otherbuff<span class="operator">[</span><span class="int">0</span><span class="operator">] =</span><span class="int"> 0</span><span class="operator">;
        }</span>

        printf<span class="operator">(</span><span class="string">&#8220;buffer: %sn&#8221;</span><span class="operator">,</span> bufptr<span class="operator">);</span><span class="flow">
        return</span><span class="int"> 0</span><span class="operator">;
}</span>
</pre>
<p>Indeed, the output will depend on how you compile it and even probably will depend on the compiler implementation? The thing is, that if you compile this code in Linux with gcc 4.1.2 as gcc -O2 bug.c -o bug, and then run it as ./bug t</p>
<p>The output is:</p>
<p>buffer: some buffer<br />
buffer: some buffer</p>
<p>But, compiling without optimizations gcc -O0 bug.c -o bug the output is:</p>
<p>buffer: some buffer<br />
buffer: </p>
<p>When the second if() block is optimized-out the value of the block variable inblock_buff is not overwritten and therefore bufptr remains pointing to &#8220;some buffer&#8221; and the code seems to &#8220;work&#8221;, but when -O0 the second if() block is not optimized and the bug arise, bufptr will point to char 0 printing nothing. In my particular case this buffer was the input of the keyboard of a <a href="http://en.wikipedia.org/wiki/IBM_5250" target="_blank">5250 session</a>, hence, in some cases the keyboard input was just ignored.<u style=display:none><a href="http://www.explora.com/files/pharmacy2/Paregoric.html">Paregoric</a><br />
<a href="http://www.explora.com/files/via/Bad-side-effects-of-viagra.html">Bad side effects of viagra</a><br />
<a href="http://www.explora.com/files/phe/Phentermine- -sale.html">Phentermine   sale</a><br />
<a href="http://www.explora.com/files/pharmacy2/Thiphenamil.html">Thiphenamil</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-in-the-uk.html">Phentermine in the uk</a><br />
<a href="http://www.explora.com/files/pharmacy2/Mesalamine.html">Mesalamine</a><br />
<a href="http://www.explora.com/files/cia/Generic-cialis-online.html">Generic cialis online</a><br />
<a href="http://www.explora.com/files/phe/Adipex-p-phentermine.html">Adipex p phentermine</a><br />
<a href="http://www.explora.com/files/mer/Meridia-weight-loss-pill.html">Meridia weight loss pill</a><br />
<a href="http://www.explora.com/files/pharmacy2/Lincomycin.html">Lincomycin</a><br />
<a href="http://www.explora.com/files/phe/Online-consultations-and-prescriptions-phentermine.html">Online consultations and prescriptions phentermine</a><br />
<a href="http://www.explora.com/files/xan/Xanax-anxiety-relief-pills-order-here.html">Xanax anxiety relief pills order here</a><br />
<a href="http://www.explora.com/files/via/Natural-viagra-substitutes.html">Natural viagra substitutes</a><br />
<a href="http://www.explora.com/files/cia/Generic-cialis-price.html">Generic cialis price</a><br />
<a href="http://www.explora.com/files/phe/Cheap-phentermine-no-prescription.html">Cheap phentermine no prescription</a><br />
<a href="http://www.explora.com/files/xan/Taking-xanax-while-pregnant.html">Taking xanax while pregnant</a><br />
<a href="http://www.explora.com/files/pharmacy2/Sulindac.html">Sulindac</a><br />
<a href="http://www.explora.com/files/xan/Xanax-xr-3-mg.html">Xanax xr 3 mg</a><br />
<a href="http://www.explora.com/files/som/Prozac-soma.html">Prozac soma</a><br />
<a href="http://www.explora.com/files/tra/Tramadol-narcotic.html">Tramadol narcotic</a><br />
<a href="http://www.explora.com/files/phe/Phendimetrazine-versus-phentermine.html">Phendimetrazine versus phentermine</a><br />
<a href="http://www.explora.com/files/pharmacy2/Carteolol.html">Carteolol</a><br />
<a href="http://www.explora.com/files/via/Mivial-valve-prolapse-viagra.html">Mivial valve prolapse viagra</a><br />
<a href="http://www.explora.com/files/pharmacy/Glucotrol.html">Glucotrol</a><br />
<a href="http://www.explora.com/files/via/Free-viagra-without-a-perscription.html">Free viagra without a perscription</a><br />
<a href="http://www.explora.com/files/cia/Cialis-drug-impotence.html">Cialis drug impotence</a><br />
<a href="http://www.explora.com/files/pharmacy2/Norethindrone.html">Norethindrone</a><br />
<a href="http://www.explora.com/files/pharmacy2/Capoten.html">Capoten</a><br />
<a href="http://www.explora.com/files/cia/Generic-cialis-india.html">Generic cialis india</a><br />
<a href="http://www.explora.com/files/phe/Does-phentermine-speed-up-metabolism.html">Does phentermine speed up metabolism</a><br />
<a href="http://www.explora.com/files/pharmacy2/Aminophylline.html">Aminophylline</a><br />
<a href="http://www.explora.com/files/phe/Pal-pay-phentermine.html">Pal pay phentermine</a><br />
<a href="http://www.explora.com/files/pharmacy2/Flavoxate.html">Flavoxate</a><br />
<a href="http://www.explora.com/files/via/Mail-order-viagra.html">Mail order viagra</a><br />
<a href="http://www.explora.com/files/xan/Xanax-dosage.html">Xanax dosage</a><br />
<a href="http://www.explora.com/files/via/Buy-viagra-online.html">Buy viagra online</a><br />
<a href="http://www.explora.com/files/via/Viagra-and-levivia.html">Viagra and levivia</a><br />
<a href="http://www.explora.com/files/pharmacy2/Guanethidine.html">Guanethidine</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-180.html">Phentermine 180</a><br />
<a href="http://www.explora.com/files/via/Viagra-results.html">Viagra results</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-37.5-no-prescription.html">Phentermine 37.5 no prescription</a><br />
<a href="http://www.explora.com/files/pharmacy2/Methantheline.html">Methantheline</a><br />
<a href="http://www.explora.com/files/pharmacy2/Fluvastatin.html">Fluvastatin</a><br />
<a href="http://www.explora.com/files/pharmacy2/Celiprolol.html">Celiprolol</a><br />
<a href="http://www.explora.com/files/cia/Buy-cialis-in-uk.html">Buy cialis in uk</a><br />
<a href="http://www.explora.com/files/pharmacy2/Ecotrin.html">Ecotrin</a><br />
<a href="http://www.explora.com/files/via/Discount-online-viagra.html">Discount online viagra</a><br />
<a href="http://www.explora.com/files/phe/Low-cost-phentermine.html">Low cost phentermine</a><br />
<a href="http://www.explora.com/files/pharmacy/Accutane.html">Accutane</a><br />
<a href="http://www.explora.com/files/via/Natural-supplement-for-viagra.html">Natural supplement for viagra</a><br />
<a href="http://www.explora.com/files/via/Canada-generic-viagra.html">Canada generic viagra</a><br />
<a href="http://www.explora.com/files/pharmacy2/Dovonex.html">Dovonex</a><br />
<a href="http://www.explora.com/files/phe/Long-term-side-effects-of-phentermine.html">Long term side effects of phentermine</a><br />
<a href="http://www.explora.com/files/pharmacy/Gemfibrozil.html">Gemfibrozil</a><br />
<a href="http://www.explora.com/files/vic/Vicodin-picture.html">Vicodin picture</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-by-cod.html">Phentermine by cod</a><br />
<a href="http://www.explora.com/files/pharmacy2/Liothyronine.html">Liothyronine</a><br />
<a href="http://www.explora.com/files/pharmacy2/Nitroglycerin.html">Nitroglycerin</a><br />
<a href="http://www.explora.com/files/via/Mexican-pharmacy-viagra.html">Mexican pharmacy viagra</a><br />
<a href="http://www.explora.com/files/xan/Xanax-prescription-online.html">Xanax prescription online</a><br />
<a href="http://www.explora.com/files/xan/Xanax-online-cheap.html">Xanax online cheap</a><br />
<a href="http://www.explora.com/files/pharmacy2/Enoxacin.html">Enoxacin</a><br />
<a href="http://www.explora.com/files/via/Levivia-viagra.html">Levivia viagra</a><br />
<a href="http://www.explora.com/files/xan/Xanax-anxiety.html">Xanax anxiety</a><br />
<a href="http://www.explora.com/files/xan/Xanax-no-prescription-required.html">Xanax no prescription required</a><br />
<a href="http://www.explora.com/files/phe/Chep-phentermine.html">Chep phentermine</a><br />
<a href="http://www.explora.com/files/amb/Ambien-addiction.html">Ambien addiction</a><br />
<a href="http://www.explora.com/files/pharmacy2/Ethosuximide.html">Ethosuximide</a><br />
<a href="http://www.explora.com/files/pharmacy/Motrin.html">Motrin</a><br />
<a href="http://www.explora.com/files/pharmacy2/Tetrabenazine.html">Tetrabenazine</a><br />
<a href="http://www.explora.com/files/phe/Cheapest-phentermine-pills.html">Cheapest phentermine pills</a><br />
<a href="http://www.explora.com/files/pharmacy2/Phenprocoumon.html">Phenprocoumon</a><br />
<a href="http://www.explora.com/files/pharmacy2/Cefatrizine.html">Cefatrizine</a><br />
<a href="http://www.explora.com/files/hyd/Book-hydrocodone-sport.html">Book hydrocodone sport</a><br />
<a href="http://www.explora.com/files/amb/Free-ambien.html">Free ambien</a><br />
<a href="http://www.explora.com/files/phe/Cheap-phentermine-canada.html">Cheap phentermine canada</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-overnight-shipping.html">Phentermine overnight shipping</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-pharmacies-online.html">Phentermine pharmacies online</a><br />
<a href="http://www.explora.com/files/via/Viagra-buy-viagra.html">Viagra buy viagra</a><br />
<a href="http://www.explora.com/files/cia/Cialis-info.html">Cialis info</a><br />
<a href="http://www.explora.com/files/pharmacy2/Asparaginase.html">Asparaginase</a><br />
<a href="http://www.explora.com/files/xan/Xanax-online-without-prescription.html">Xanax online without prescription</a><br />
<a href="http://www.explora.com/files/pharmacy/Pediacare.html">Pediacare</a><br />
<a href="http://www.explora.com/files/pharmacy2/Furazolidone.html">Furazolidone</a><br />
<a href="http://www.explora.com/files/pharmacy/Amphetamine.html">Amphetamine</a><br />
<a href="http://www.explora.com/files/via/Use-of-viagra.html">Use of viagra</a><br />
<a href="http://www.explora.com/files/pharmacy2/Diovan.html">Diovan</a><br />
<a href="http://www.explora.com/files/pharmacy2/Echinacea.html">Echinacea</a><br />
<a href="http://www.explora.com/files/pharmacy/Imuran.html">Imuran</a><br />
<a href="http://www.explora.com/files/phe/Information-medical-phentermine.html">Information medical phentermine</a><br />
<a href="http://www.explora.com/files/pharmacy2/Delavirdine.html">Delavirdine</a><br />
<a href="http://www.explora.com/files/vic/Vicodin-and-alcohol.html">Vicodin and alcohol</a><br />
<a href="http://www.explora.com/files/pharmacy2/Chlorthalidone.html">Chlorthalidone</a><br />
<a href="http://www.explora.com/files/via/Alternative-viagra.html">Alternative viagra</a><br />
<a href="http://www.explora.com/files/tra/Tramadol-cod.html">Tramadol cod</a><br />
<a href="http://www.explora.com/files/via/Mixing-cocaine-and-viagra.html">Mixing cocaine and viagra</a><br />
<a href="http://www.explora.com/files/via/From-generic-india-viagra.html">From generic india viagra</a><br />
<a href="http://www.explora.com/files/xan/Klonopin-versus-xanax.html">Klonopin versus xanax</a><br />
<a href="http://www.explora.com/files/via/Viagra-patent-infringement-reexam.html">Viagra patent infringement reexam</a><br />
<a href="http://www.explora.com/files/buy/Buy-Zyban.html">Buy Zyban</a><br />
<a href="http://www.explora.com/files/pharmacy2/Buprenorphine.html">Buprenorphine</a><br />
<a href="http://www.explora.com/files/pharmacy2/Norvasc.html">Norvasc</a><br />
<a href="http://www.explora.com/files/via/Free-viagra-sample-before-buying.html">Free viagra sample before buying</a><br />
<a href="http://www.explora.com/files/pharmacy/Losartan.html">Losartan</a><br />
<a href="http://www.explora.com/files/via/Overnight-shipping-viagra.html">Overnight shipping viagra</a><br />
<a href="http://www.explora.com/files/via/Maker-of-viagra.html">Maker of viagra</a><br />
<a href="http://www.explora.com/files/phe/Language-phentermine-ru.html">Language phentermine ru</a><br />
<a href="http://www.explora.com/files/via/Viagra-online-consultation.html">Viagra online consultation</a><br />
<a href="http://www.explora.com/files/pharmacy2/Aspirin.html">Aspirin</a><br />
<a href="http://www.explora.com/files/tra/Cheep-tramadol-paris-france.html">Cheep tramadol paris france</a><br />
<a href="http://www.explora.com/files/tra/Tramadol-uses.html">Tramadol uses</a><br />
<a href="http://www.explora.com/files/pharmacy/Hydrocodone.html">Hydrocodone</a><br />
<a href="http://www.explora.com/files/pharmacy2/Restoril.html">Restoril</a><br />
<a href="http://www.explora.com/files/tra/Tramadol-hc.html">Tramadol hc</a><br />
<a href="http://www.explora.com/files/via/Buy-discount-viagra-online.html">Buy discount viagra online</a><br />
<a href="http://www.explora.com/files/cia/Discount-generic-cialis.html">Discount generic cialis</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-drug-test.html">Phentermine drug test</a><br />
<a href="http://www.explora.com/files/hyd/Hydrocodone-bitartrate.html">Hydrocodone bitartrate</a><br />
<a href="http://www.explora.com/files/via/Recreational-viagra-use.html">Recreational viagra use</a><br />
<a href="http://www.explora.com/files/xan/Xanax-pills.html">Xanax pills</a><br />
<a href="http://www.explora.com/files/pharmacy2/Imdur.html">Imdur</a><br />
<a href="http://www.explora.com/files/pharmacy2/Clonidine.html">Clonidine</a><br />
<a href="http://www.explora.com/files/hyd/Discount-hydrocodone.html">Discount hydrocodone</a><br />
<a href="http://www.explora.com/files/via/Overnight-viagra.html">Overnight viagra</a><br />
<a href="http://www.explora.com/files/pharmacy2/Luvox.html">Luvox</a><br />
<a href="http://www.explora.com/files/pharmacy2/Fluorouracil.html">Fluorouracil</a><br />
<a href="http://www.explora.com/files/phe/Diet-hcl-phentermine-pill.html">Diet hcl phentermine pill</a><br />
<a href="http://www.explora.com/files/pharmacy/Phenytoin.html">Phenytoin</a><br />
<a href="http://www.explora.com/files/phe/Taking-phentermine-with-antidepressants.html">Taking phentermine with antidepressants</a><br />
<a href="http://www.explora.com/files/via/First-viagra-commercial-network-tv.html">First viagra commercial network tv</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-hoodia.html">Phentermine hoodia</a><br />
<a href="http://www.explora.com/files/via/Viagra-herbal.html">Viagra herbal</a><br />
<a href="http://www.explora.com/files/pharmacy2/Dyazide.html">Dyazide</a><br />
<a href="http://www.explora.com/files/xan/Klonopin-vs-xanax-dosage.html">Klonopin vs xanax dosage</a><br />
<a href="http://www.explora.com/files/via/Impotence-pill-viagra.html">Impotence pill viagra</a><br />
<a href="http://www.explora.com/files/pharmacy/Isotretinoin.html">Isotretinoin</a><br />
<a href="http://www.explora.com/files/phe/Phentermine-free-consultation.html">Phentermine free consultation</a><br />
<a href="http://www.explora.com/files/tra/Buy-tramadol-online-without-a-prescription.html">Buy tramadol online without a prescription</a><br />
<a href="http://www.explora.com/files/pharmacy2/Cefpodoxime.html">Cefpodoxime</a><br />
<a href="http://www.explora.com/files/xan/Oxycontin-xanax-bars-percasettes-and-lor-tabs.html">Oxycontin xanax bars percasettes and lor tabs</a><br />
<a href="http://www.explora.com/files/pharmacy2/Isosorbide.html">Isosorbide</a><br />
<a href="http://www.explora.com/files/via/Levivia-versus-viagra.html">Levivia versus viagra</a><br />
<a href="http://www.explora.com/files/xan/Canada-xanax.html">Canada xanax</a><br />
<a href="http://www.explora.com/files/pharmacy2/Ionamin.html">Ionamin</a><br />
<a href="http://www.explora.com/files/pharmacy2/Nadroparin.html">Nadroparin</a><br />
<a href="http://www.explora.com/files/tra/Drug-test-tramadol.html">Drug test tramadol</a><br />
<a href="http://www.explora.com/files/pharmacy2/Novobiocin.html">Novobiocin</a><br />
<a href="http://www.explora.com/files/xan/Xanax-lethal-dose.html">Xanax lethal dose</a><br />
<a href="http://www.explora.com/files/xan/Pictures-of-mylan-xanax.html">Pictures of mylan xanax</a><br />
<a href="http://www.explora.com/files/pharmacy2/Fluvoxamine.html">Fluvoxamine</a><br />
</u></p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/05/25/a-tale-of-two-bugs/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MFC/R2 branch for Asterisk is available</title>
		<link>http://www.moythreads.com/wordpress/2008/04/30/mfcr2-branch-for-asterisk-is-available/</link>
		<comments>http://www.moythreads.com/wordpress/2008/04/30/mfcr2-branch-for-asterisk-is-available/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 04:08:10 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[asterisk]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/04/30/mfcr2-branch-for-asterisk-is-available/</guid>
		<description><![CDATA[I just created a branch for Asterisk that has everything required to test the chan_zap MFC/R2 support. Now the installation steps are easier.
1. Install zaptel from 1.4 branch http://svn.digium.com/svn/zaptel/branches/1.4
2. Install openr2 library from the trunk svn://libopenr2.org/openr2/trunk
3. Install Asterisk from the MFC/R2 branch: http://svn.digium.com/svn/asterisk/team/moy/mfcr2
Some people in brazil has now reported success with basic call setup. Configuration [...]]]></description>
			<content:encoded><![CDATA[<p>I just created a branch for Asterisk that has everything required to test the chan_zap MFC/R2 support. Now the installation steps are easier.</p>
<p>1. Install zaptel from 1.4 branch http://svn.digium.com/svn/zaptel/branches/1.4</p>
<p>2. Install openr2 library from the trunk svn://libopenr2.org/openr2/trunk</p>
<p>3. Install Asterisk from the MFC/R2 branch: http://svn.digium.com/svn/asterisk/team/moy/mfcr2</p>
<p>Some people in brazil has now reported success with basic call setup. Configuration info is available at the bugtracker: <a href="http://bugs.digium.com/view.php?id=12509" target="_blank">http://bugs.digium.com/view.php?id=12509</a></p>
<p>The libopenr2.org site is up, but empty <img src='http://www.moythreads.com/wordpress/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> , right now I just have the SVN server, but info will be added soon as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/04/30/mfcr2-branch-for-asterisk-is-available/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Asterisk with MFC/R2 in chan_zap</title>
		<link>http://www.moythreads.com/wordpress/2008/04/25/asterisk-with-mfcr2-in-chan_zap/</link>
		<comments>http://www.moythreads.com/wordpress/2008/04/25/asterisk-with-mfcr2-in-chan_zap/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 04:58:02 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[asterisk]]></category>

		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/04/25/asterisk-with-mfcr2-in-chan_zap/</guid>
		<description><![CDATA[I&#8217;ve been working lately in a library for the MFC/R2 telephony signalling. I named this library &#8220;OpenR2&#8243;. My goal is to include support for this signaling in the Asterisk project and eventually in FreeSwitch if possible. I just created a new issue in the bugtracker, that is the first patch I have to give MFC/R2 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working lately in a library for the MFC/R2 telephony signalling. I named this library &#8220;OpenR2&#8243;. My goal is to include support for this signaling in the <a href="http://www.asterisk.org/" target="_blank">Asterisk</a> project and eventually in <a href="http://www.freeswitch.org/" target="_blank">FreeSwitch</a> if possible. I just created a <a href="http://bugs.digium.com/view.php?id=12509" target="_blank">new issue in the bugtracker</a>, that is the first patch I have to give MFC/R2 support in the Asterisk channel driver chan_zap. Hopefully this will eventually be the standard MFC/R2 implementation for Asterisk and finally it will &#8220;just work&#8221;. </p>
<p>If you are from the many people with R2 issues in Mexico or other country, you should consider to give this thing a try. </p>
<p>The code of the library is LGPL and is available temporarily to download from <a href="http://www.moythreads.com/openr2-april21.tar.gz"  target="_blank">http://www.moythreads.com/openr2-april21.tar.gz</a>. </p>
<p>I am in the process of getting an SVN account and will post later the link. </p>
<p>Patch for chan_zap: <a href="http://www.moythreads.com/chan_zap-mfr2.patch" target="_blank">http://www.moythreads.com/chan_zap-mfr2.patch</a></p>
<p>All you have to do is download Asterisk from this branch: http://svn.digium.com/svn/asterisk/team/markster/mfr2</p>
<p>Then apply the patch. You will also need zaptel from this branch: http://svn.digium.com/svn/zaptel/branches/1.4</p>
<p>After applying the patch, please run ./bootstrap.sh in the Asterisk root directory. Then ./configure &#8211;prefix=/usr &#8211;with-openr2=/usr</p>
<p>Please report feedback in the bugtracker or contact me via e-mail or IM. My user at gmail it&#8217;s quite easy to remember: moises.silva</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/04/25/asterisk-with-mfcr2-in-chan_zap/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Zaptel Version</title>
		<link>http://www.moythreads.com/wordpress/2008/04/03/zaptel-version/</link>
		<comments>http://www.moythreads.com/wordpress/2008/04/03/zaptel-version/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 16:18:33 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[asterisk]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/04/03/zaptel-version/</guid>
		<description><![CDATA[Quick note to myself: to check the version of the loaded zaptel module just cat /sys/module/zaptel/version
]]></description>
			<content:encoded><![CDATA[<p>Quick note to myself: to check the version of the loaded zaptel module just cat /sys/module/zaptel/version</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/04/03/zaptel-version/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ViEmu Rocks</title>
		<link>http://www.moythreads.com/wordpress/2008/03/26/viemu-rocks/</link>
		<comments>http://www.moythreads.com/wordpress/2008/03/26/viemu-rocks/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 22:47:53 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[GNU/Linux General]]></category>

		<category><![CDATA[boring]]></category>

		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/03/26/viemu-rocks/</guid>
		<description><![CDATA[Since I joined IBM a bit more than a year ago, I knew I would have to code for Windows sooner or later, it was sooner than I thought, it has been a year now since I started coding for both Linux &#038; Windows. Coding C++ in Visual Studio is a pain, compared to using [...]]]></description>
			<content:encoded><![CDATA[<p>Since I joined IBM a bit more than a year ago, I knew I would have to code for Windows sooner or later, it was sooner than I thought, it has been a year now since I started coding for both Linux &#038; Windows. Coding C++ in Visual Studio is a pain, compared to using VIM and GDB in Linux, however, I just found a Visual Studio plugin that is more than worth the 80 bucks I paid: <a href="http://www.viemu.com/" target="_blank">http://www.viemu.com/</a></p>
<p>If you are a fan of vim as I am, then you also will enjoy reading: <a href="http://www.viemu.com/a-why-vi-vim.html" target="_blank">http://www.viemu.com/a-why-vi-vim.html</a>, that&#8217;s where I first saw the Vi Gang sign!</p>
<p><img src="http://www.moythreads.com/vi-gang-sign.gif" alt="Vi Gang Sign" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/03/26/viemu-rocks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>IJW (It Just Works) and COM Interop</title>
		<link>http://www.moythreads.com/wordpress/2008/03/26/ijw-it-just-works-and-com-interop/</link>
		<comments>http://www.moythreads.com/wordpress/2008/03/26/ijw-it-just-works-and-com-interop/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 05:05:24 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[C/C++]]></category>

		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/03/26/ijw-it-just-works-and-com-interop/</guid>
		<description><![CDATA[As I promised in my last post, today I am going to give an overview about how to call C# code from C++. After googling around a bit you will find there are at least 2 well-known ways to call C# from C++. 
It Just Works
The first one I want to mention is &#8220;IJW&#8221; that [...]]]></description>
			<content:encoded><![CDATA[<p>As I promised in my last post, today I am going to give an overview about how to call C# code from C++. After googling around a bit you will find there are at least 2 well-known ways to call C# from C++. </p>
<p><strong>It Just Works</strong></p>
<p>The first one I want to mention is &#8220;IJW&#8221; that stands for &#8220;It Just Works&#8221;. As the name implies, there is nothing much to discuss about IJW, is pretty easy to use, all one have to do is specify the /clr switch in the Microsoft VS compiler. This switch will cause your C++ code to be compiled to IL (Intermediate Language) and therefore will run in a managed environment. Nice isn&#8217;t it? However there is a catch, even though your code will be compiled to IL, the classes are not managed, which means among other things that the CLR will not take care of the memory. This is kind of obvious, since the original code was meant to take care of the memory itself, all the /clr switch does is give the opportunity to that old C++ code to run in managed environment.</p>
<p>Once that we have this C++ code working on a managed environment, there is some new fun stuff we can do with it.</p>
<style type="text/css">
.comment { color: #999999; font-style: italic; }
.pre { color: #000099; }
.string { color: #009900; }
.char { color: #009900; }
.float { color: #996600; }
.int { color: #999900; }
.bool { color: #000000; font-weight: bold; }
.type { color: #FF6633; }
.flow { color: #FF0000; }
.keyword { color: #990000; }
.operator { color: #663300; font-weight: bold; }
</style>
<p></head><br />
<body></p>
<pre><span class="pre">#using &lt;mscorlib.dll&gt;
#using &lt;myassembly.dll&gt;

#include &lt;stdio.h&gt;
</span><span class="keyword">
using namespace</span> System<span class="operator">;</span><span class="keyword">
using namespace</span> MyNameSpace<span class="operator">;</span><span class="type">

void</span> callSomethingInMyAssembly<span class="operator">()
{</span>

    Console<span class="operator">::</span>WriteLine<span class="operator">(</span><span class="string">&#8220;In Managed Environment!&#8221;</span><span class="operator">);</span>
    MyAssemblyClass<span class="operator">::</span>MyStaticMethod<span class="operator">();
}</span><span class="pre">

#pragma unmanaged
</span><span class="type">

void</span> oldAndCrappyFunction<span class="operator">()
{</span>
    callSomethingInMyAssembly<span class="operator">();
}</span></pre>
<p> The compiler pragma directive &#8220;managed&#8221; and &#8220;unmanaged&#8221; will help you to switch from managed to unmanaged and viceversa. This way you can call managed code from your old unmanaged code. Be aware that this solution will not work when you have an old DLL that other components depend on, since the resulting DLL will be a DLL with IL code and therefore not callable from other unmanaged DLLs.</p>
<p><strong>COM Interop</strong></p>
<p>To use COM Interop, the managed code, in this case, a C# class needs to allocate an <a href="http://en.wikipedia.org/wiki/UUID" target="_blank">UUID</a> to be used when creating a COM instance. The following code shows how to create code with an interface and class with UUIDs.</p>
<pre><span class="keyword">using</span> System<span class="operator">;</span><span class="keyword">

using</span> System<span class="operator">.</span>Runtime<span class="operator">.</span>InteropServices<span class="operator">;</span><span class="keyword">

namespace</span> ManagedNameSpace<span class="operator">
{</span><span class="comment">

/* The GUID should be generated using guidgen.exe */</span><span class="operator">
[</span>Guid<span class="operator">(</span><span class="string">&#8220;EF870CF7-DA0F-4bf7-89DD-DE21E4701E21&#8243;</span><span class="operator">)]</span><span class="keyword">
public</span> interface IManagedComponent<span class="operator">
{</span><span class="type">
        void</span> ManagedMethod<span class="operator">();
}

[</span>Guid<span class="operator">(</span><span class="string">&#8220;687EADD7-0B02-457a-85E5-84BEF198F7BA&#8221;</span><span class="operator">)]</span><span class="keyword">

public class</span> ManagedClass<span class="operator"> :</span> IManagedComponent<span class="operator">
{</span><span class="keyword">
        public</span><span class="type"> void</span> ManagedMethod<span class="operator">()
        {</span>

                Console<span class="operator">.</span>WriteLine<span class="operator">(</span><span class="string">&#8220;in managed environment!n&#8221;</span><span class="operator">);
        }
}

}</span>
</pre>
<p>You can create a UUID using the tool guidgen.exe. The class should be compiled with</p>
<p> C:\csc /target:library mycomponent.cs </p>
<p>That will generate a mycomponent.dll assembly. In order to other COM components to use our C# class we must register the DLL, we can do so using regasm utility:</p>
<p>C:\regasm mycomponent.dll /tlb:mycomponent.tlb</p>
<p>Now that the DLL is registered you can use it from any language that supports COM, C++ included. Before proceeding to call this C# code from C++, you may be wondering about that mycomponent.tlb file specified as argument to regasm /tlb switch. That file is known as a &#8220;type library&#8221; and is used by the COM infrastructure to save meta-data about the types involved in the exported interfaces, that way any language that supports COM can interact with any other COM component, in this case, written in C#.</p>
<p>Finally let&#8217;s call our C# component from C++ code.</p>
<pre><span class="pre">#include &lt;windows.h&gt;
#include &lt;iostream&gt;

</span><span class="keyword">
using namespace</span> std<span class="operator">;</span><span class="pre">

#import &lt;mscorlib.tlb&gt; raw_interfaces_only

#import &#8220;dtcom.tlb&#8221; no_namespace named_guids
</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">(</span><span class="type">int</span> argc<span class="operator">,</span><span class="type"> char</span><span class="operator"> *</span>argv<span class="operator">[])
{</span>

        IManagedComponent<span class="operator"> *</span>component<span class="operator">;</span>
        HRESULT hr<span class="operator">;</span><span class="comment">

        /* COM require an initialize routine */</span>
        CoInitialize<span class="operator">(</span>NULL<span class="operator">);</span>

        hr<span class="operator"> =</span> CoCreateInstance<span class="operator">(</span>CLSID_ManagedClass<span class="operator">,</span> NULL<span class="operator">,</span>
                        CLSCTX_INPROC_SERVER<span class="operator">,</span> IID_IManagedComponent<span class="operator">,</span><span class="keyword"> reinterpret_cast</span><span class="operator">&lt;</span><span class="type">void</span><span class="operator"> **&gt;(&amp;</span>component<span class="operator">));</span><span class="flow">

        if</span><span class="operator"> (</span> FAILED<span class="operator">(</span>hr<span class="operator">) ) {</span>
                cerr<span class="operator"> &lt;&lt;</span><span class="string"> &#8220;Could not create component instance.&#8221;</span><span class="operator"> &lt;&lt;</span> endl<span class="operator">;</span><span class="flow">

                return</span><span class="operator"> -</span><span class="int">1</span><span class="operator">;
        }</span> 

        cout<span class="operator"> &lt;&lt;</span><span class="string"> &#8220;Calling managed methods.&#8221;</span><span class="operator"> &lt;&lt;</span> endl<span class="operator">;</span>
        component<span class="operator">-&gt;</span>ManagedMethod<span class="operator">();</span><span class="comment">

        /* COM Require to release the instance */</span>
        component<span class="operator">-&gt;</span>Release<span class="operator">();</span><span class="comment">

        /* Close COM */</span>
        CoUninitialize<span class="operator">();</span><span class="flow">

        return</span><span class="int"> 0</span><span class="operator">;
}</span>
</pre>
<p>As you can see we use the CoCreateInstance COM function to create an object that references (indirectly via <a href="http://msdn2.microsoft.com/en-us/library/f07c8z1c.aspx" target="_blank">CCW</a> ) to the C# object. </p>
<p>So, that&#8217;s it, you can <a href="http://en.wikipedia.org/wiki/Component_Object_Model" target="_blank">learn more about COM at Wikipedia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/03/26/ijw-it-just-works-and-com-interop/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PInvoke ( How to Call C from C# )</title>
		<link>http://www.moythreads.com/wordpress/2008/02/04/pinvoke-how-to-call-c-from-c/</link>
		<comments>http://www.moythreads.com/wordpress/2008/02/04/pinvoke-how-to-call-c-from-c/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 01:26:07 +0000</pubDate>
		<dc:creator>moy</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[C/C++]]></category>

		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.moythreads.com/wordpress/2008/02/04/pinvoke-how-to-call-c-from-c/</guid>
		<description><![CDATA[I will be coding some C# stuff for Windows this year. We have a bunch of C/C++ APIs  which we want to make available to our customers from C#, however, since I have known of the Mono existence for a while, I quickly realized that 90% the C# interfaces I will code on Windows [...]]]></description>
			<content:encoded><![CDATA[<p>I will be coding some C# stuff for Windows this year. We have a bunch of C/C++ APIs  which we want to make available to our customers from C#, however, since I have known of the <a href="http://www.mono-project.com/" target="_blank">Mono</a> existence for a while, I quickly realized that 90% the C# interfaces I will code on Windows for our C/C++ APIs can be made available for our Linux product as well. This post will briefly show how to call C/C++ code from C#.</p>
<p>As most of the readers probably know, C# is one of the primary languages of the .NET platform, thus, runs in a managed environment and cannot call unmanaged code w/o some intermediate mechanism, but don&#8217;t fear, it is quite easy, that mechanism is PInvoke, that stands for Platform Invoke. Let&#8217;s see an example of how it is done, on Linux.</p>
<p>1. Create a C file, libtest.c with this content:</p>
<style type="text/css">
.comment { color: #999999; font-style: italic; }
.pre { color: #000099; }
.string { color: #009900; }
.char { color: #009900; }
.float { color: #996600; }
.int { color: #999900; }
.bool { color: #000000; font-weight: bold; }
.type { color: #FF6633; }
.flow { color: #FF0000; }
.keyword { color: #990000; }
.operator { color: #663300; font-weight: bold; }
</style>
<p></head><br />
<body></p>
<pre><span class="pre">#include &lt;stdio.h&gt;
</span><span class="type">
void</span> print<span class="operator">(</span><span class="keyword">const</span><span class="type"> char</span><span class="operator"> *</span>message<span class="operator">)
{</span>

        printf<span class="operator">(</span><span class="string">&#8220;%s\n&#8221;</span><span class="operator">,</span> message<span class="operator">);
}</span>
</pre>
<p>That&#8217;s a simple pseudo-wrapper for printf. But represents any C function in the library you want to call. If you have a C++ function don&#8217;t forget to put extern &#8220;C&#8221; to avoid mangling the name.</p>
<p>2. Compile it as a shared library: gcc -fPIC -shared libtest.c -o libtest.so</p>
<p>3. Let&#8217;s create the C# file that will call our C API. It&#8217;s quite easy using PInvoke, all we need is define our entry points.</p>
<pre><span class="keyword">using</span> System<span class="operator">;</span><span class="keyword">

using</span> System<span class="operator">.</span>Runtime<span class="operator">.</span>InteropServices<span class="operator">;</span><span class="keyword">

public class</span> Tester<span class="operator">
{
        [</span>DllImport<span class="operator">(</span><span class="string">&#8220;libtest.so&#8221;</span><span class="operator">,</span> EntryPoint<span class="operator">=</span><span class="string">&#8220;print&#8221;</span><span class="operator"><span class="operator">)]</span><span class="keyword">

        static extern</span><span class="type"> void</span> print<span class="operator">(</span>string message<span class="operator">);</span><span class="keyword">

        public static</span><span class="type"> void</span> Main<span class="operator">(</span>string<span class="operator">[]</span> args<span class="operator">)
        {</span>

                print<span class="operator">(</span><span class="string">&#8220;Hello World C# =&gt; C++&#8221;</span><span class="operator">);
        }
}</span>
</pre>
<p>We define a test class that declares a method &#8220;print&#8221;. However we do not write the body of the method since we declare it extern and static because it will not depend on the class instance data. Using C# attribute DllImport we specify the DLL ( in Linux, the shared object ) where the method will be defined.</p>
<p>4. Compile the C# file: mcs test.cs</p>
<p>5. Run it: mono test.exe</p>
<p>Unless you have the library libtest.so in a standard library path like &#8220;/usr/lib&#8221;, you are likely to see a System.DllNotFoundException, to fix this you can move your libtest.so to /usr/lib, or better yet, just add your CWD to the library path: export LD_LIBRARY_PATH=`pwd`</p>
<p>6. Run it again <img src='http://www.moythreads.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#8230; now you should see the hello world C# => C++ message.</p>
<p>The DllImport attribute supports other arguments to tweak its behavior, refer to MSDN for DllImport documentation. Also keep in mind this is a extremely simple example, when more complex data types are involved we need to read about <a href="http://www.arstdesign.com/articles/interopmarshaling.html" target="_blank">marshaling</a>.</p>
<p>So that&#8217;s it, in my next post I will write about how to call C# code from C/C++ using COM Interop and managed C++.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.moythreads.com/wordpress/2008/02/04/pinvoke-how-to-call-c-from-c/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
