<?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>Tamir Khason - Just code &#187; Mono</title>
	<atom:link href="http://khason.net/tag/mono/feed/" rel="self" type="application/rss+xml" />
	<link>http://khason.net</link>
	<description>Take care of the sense, and the sounds will take care of themselves.</description>
	<lastBuildDate>Thu, 15 Apr 2010 13:25:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>P/Invoke cheat sheet</title>
		<link>http://khason.net/blog/pinvoke-cheat-sheet/</link>
		<comments>http://khason.net/blog/pinvoke-cheat-sheet/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 16:02:40 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DEV]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF crossbow]]></category>
		<category><![CDATA[x64]]></category>

		<guid isPermaLink="false">http://khason.net/blog/pinvoke-cheat-sheet/</guid>
		<description><![CDATA[I’m working a lot with p/invoke, and know how it’s hard to produce correct signature for unmanaged method. So, today I decided to publish basic cheat sheet for methods, parameters and attributes you should use in order to invoke unmanaged methods from managed code without a lot of problems. We start with data type translations. [...]


Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I’m working a lot with p/invoke, and know how it’s hard to produce correct signature for unmanaged method. So, today I decided to publish basic cheat sheet for methods, parameters and attributes you should use in order to invoke unmanaged methods from managed code without a lot of problems. We start with data type translations. Here the table to understand it.</p>
<table cellspacing="0" cellpadding="2" width="600" border="0">
<tbody>
<tr>
<td valign="top" width="300">Data type from unmanaged signature</td>
<td valign="top" width="300">Data type in managed signature</td>
</tr>
<tr>
<td valign="top" width="300">int</td>
<td valign="top" width="300">int         </p>
<p><em>the same with all other simple types such as double, uint, etc or private objects</em></td>
</tr>
<tr>
<td valign="top" width="300">void*</td>
<td valign="top" width="300">IntPtr</td>
</tr>
<tr>
<td valign="top" width="300">int*</td>
<td valign="top" width="300">ref int         </p>
<p><em>the same with all other simple types such as double, uint, etc </em><em>or private objects</em> </td>
</tr>
<tr>
<td valign="top" width="300">char**</td>
<td valign="top" width="300">ref IntPtr         </p>
<p><em>later, you should get ascii string by using System.Runtime.InteropServices.Marshal.PtrToStringAnsi() method</em></td>
</tr>
<tr>
<td valign="top" width="300">wcar_t**</td>
<td valign="top" width="300">ref IntPtr         </p>
<p><em>later, you should get ascii string by using System.Runtime.InteropServices.Marshal.PtrToStringUni() method</em></td>
</tr>
<tr>
<td valign="top" width="300">const int*</td>
<td valign="top" width="300">ref int</td>
</tr>
<tr>
<td valign="top" width="300">const char*</td>
<td valign="top" width="300">[System.Runtime.InteropServices.In()] [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string</td>
</tr>
<tr>
<td valign="top" width="300">… <em>(variable argument)</em></td>
<td valign="top" width="300">[System.Runtime.InteropServices.In()] [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.AsAny)] object</td>
</tr>
</tbody>
</table>
<p><em>You can use either System.Runtime.InteropServices.In or System.Runtime.InteropServices.Out attribute to specify how arguments should be used.</em></p>
<p>Now we done with simple arguments, let’s see what can be done when argument is actually callback or delegate?</p>
<table cellspacing="0" cellpadding="2" width="600" border="0">
<tbody>
<tr>
<td valign="top" width="300">Unmanaged definition</td>
<td valign="top" width="300">Managed definition</td>
</tr>
<tr>
<td valign="top" width="300">typedef void (*MyCallback)(int Arg)</td>
<td valign="top" width="300">
<p>[System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl)]delegate void MyCallback(int Arg)           <br /><em>Caller cleans stack argument is used to assure, that we can call varargs type function, usually used by API provider. It is very similar to C# overrides for methods. Also you can use StdCall (this is default), ThisCall – stores this first and pushes other parameters on the stack, FastCall – not very supported <img src='http://khason.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </em></p>
</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<p>To call all those methods, we should know managed equivalents of unmanaged types. Here the table. The rule is simple – know how many bytes unmanaged type has and find managed type with the same number of bytes. Other words, you can marshal int into IntPtr too…</p>
<table cellspacing="0" cellpadding="2" width="600" border="0">
<tbody>
<tr>
<td valign="top" width="300">Unmanaged type</td>
<td valign="top" width="300">Managed equivalent</td>
</tr>
<tr>
<td valign="top" width="300">bool</td>
<td valign="top" width="300">bool</td>
</tr>
<tr>
<td valign="top" width="300">char</td>
<td valign="top" width="300">sbyte (signed), byte (unsigned)</td>
</tr>
<tr>
<td valign="top" width="300">wchar_t</td>
<td valign="top" width="300">char</td>
</tr>
<tr>
<td valign="top" width="300">double</td>
<td valign="top" width="300">double</td>
</tr>
<tr>
<td valign="top" width="300">float</td>
<td valign="top" width="300">single</td>
</tr>
<tr>
<td valign="top" width="300">int, long (signed)</td>
<td valign="top" width="300">Int32</td>
</tr>
<tr>
<td valign="top" width="300">int, long (unsigned)</td>
<td valign="top" width="300">UInt32</td>
</tr>
<tr>
<td valign="top" width="300">__int64 (signed)</td>
<td valign="top" width="300">Int64</td>
</tr>
<tr>
<td valign="top" width="300">__int64</td>
<td valign="top" width="300">UInt64</td>
</tr>
<tr>
<td valign="top" width="300">short (signed)</td>
<td valign="top" width="300">Int16</td>
</tr>
<tr>
<td valign="top" width="300">short (unsigned)</td>
<td valign="top" width="300">UInt16</td>
</tr>
<tr>
<td valign="top" width="300">void</td>
<td valign="top" width="300">void</td>
</tr>
</tbody>
</table>
<p>But not only types are problem in managed/unmanaged transitions. Also structures are aligned differently. For this purpose we can use StructLayout attribute. Even if unmanaged classes are sequential and you used correct managed data types, you can find you with problems in Pack. What “pack” is? Pack is actually slot size in bytes for members of your structure. It can be 0, 1, 2, 4, 8, 16, 32, 64, or 128 and depends on the platform and application setting.</p>
<p>Now you can see, that it is not very complicated to create managed signatures when you have header of unmanaged assemblies. So go ahead and ask, if I missed something.</p>
<p>That’s all by now. Have a nice day and be good people.</p>


<p>Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/pinvoke-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Configuring and running Mono ASP.NET 3.5 (AJAX.NET) on Linux computers</title>
		<link>http://khason.net/blog/configuring-and-running-mono-aspnet-35-ajaxnet-on-linux-computers/</link>
		<comments>http://khason.net/blog/configuring-and-running-mono-aspnet-35-ajaxnet-on-linux-computers/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 13:27:10 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[teched]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>

		<guid isPermaLink="false">http://khason.net/blog/configuring-and-running-mono-aspnet-35-ajaxnet-on-linux-computers/</guid>
		<description><![CDATA[Before we will start, we should install Linux. To do this, you can download any of LiveCDs with live installation. Officially, Mono supported only on one free Linux &#8211; openSuse. However, you can make it work on any RedHat (and its alternatives), OpenSolaris. It works, but unsupported on Debians, Ubuntu and Maemo. We’ll stick to [...]


Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Before we will start, we should install Linux. To do this, you can download any of LiveCDs with live installation. Officially, Mono supported only on one free Linux &#8211; <a href="http://software.opensuse.org/">openSuse</a>. However, you can make it work on any RedHat (and its alternatives), <a href="http://opensolaris.com/get">OpenSolaris</a>. It works, but unsupported on Debians, Ubuntu and Maemo. We’ll stick to openSuse by now. </p>
<p><img title="image" border="0" alt="image" src="http://khason.net/images/2008/12/image-3947ac10-c1ff-4b7b-bcbf-3b74b1d4b29b.png" width="347" height="155" /> </p>
<h3>Let’s install it &#8211; OS</h3>
<p>I’m assuming, that you’re installing Linux as the only OS on the machine, so insert liveCD (it can be either Gnome or KDE) and wait for Linux to run. It will run live image directly on your machine without installation.</p>
<p>When it’ll up, you’ll see liveInstall icon in your desktop. Click it and skip first two screens (it is only language and local settings). Next screen (disk partitions) is necessary for us. </p>
<p>On this screen, first delete all automatic partitions. The only one main partition will remain (/DEV/SDA or /DEV/HDA). Next you should choose non-LVM option and then start creating partitions.</p>
<p>Create first partition with <em>mount point</em> <strong>/boot</strong> and <em>size</em> of <strong>100Mb</strong>. <em>File system</em> for this partition should be <strong>ext3</strong>.</p>
<p>Create second partition with <em>file system</em> <strong>SWAP</strong> (you will not have <em>mount point</em>) and set the <em>size</em> <strong>twice bigger, then RAM</strong> amount.</p>
<p>Create last partition with <em>mount point</em> <strong>/</strong> and all <strong>remaining size on disk</strong>.</p>
<p>All other steps are optional, you can just click Next button.</p>
<p>After about 10 minutes you’ll have up and running openSuse system. (If you forgot to remove CD, choose HardDisk as boot option)</p>
<h3>Web Server installation</h3>
<p>Now we have to install web server. You can choose either <strong>Apache</strong>, <strong>FastCGI</strong>&#160; or use build-in server within Mono – <strong>XSP</strong>. We’ll choose Apache</p>
<p>Goto “Computer” it’s in the same place as Start button <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  and choose <strong>YaST</strong>. You’ll be asked for admin password, you entered while installing the system.    </p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
<p>Now in the <em>Filter </em>field, type “Install”. Choose “Software Management” from the available programs at right. Now, when Package Selection dialog opens, type “apache”, you’ll find <strong>apache2</strong>. Select it and click Install. Apache will move to the right column. Optionally, you can install also prefork and utils packages.</p>
<p>Now hit “Apply” to install it. Within two minutes, you’ll be asked to log off and log on. Do it.</p>
<p>By now apache is not running, you should run it and set it starts automatically. To do this, enter terminal window (you can either do it from “Computer” menu or right clicking desktop).</p>
<p>You need elevation to administrate startup programs. So type: “<strong>su –</strong>“ and enter your password. Terminal color turns red. Type “<strong>chkconfig apache2 on</strong>”. Now you should check whether it done, so type: “<strong>chkconfig apache2 –list</strong>”. You should see “On” near <strong>number</strong> <strong>3</strong> and <strong>5</strong>.</p>
<p>To run apache manually, just type “<strong>/etc/init.d/apache2 start</strong>” to stop “<strong>/etc/init.d/apache2 stop</strong>”, to restart “<strong>/etc/init.d/apache2 restart</strong>” and to check the status “<strong>/etc/init.d/apache2 status</strong>”</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
<p>We done, apache is up and running. Now we should install mono</p>
<h3>Mono installation</h3>
<p>Start with the same <em>YaST<strong> </strong></em>but this time, type “mono” – you’&#8217;ll get a lot of programs. To simplified installation, choose (or type) <strong>mono-complete</strong>. This will all available Mono modules.</p>
<p>After Mono will be installed, you should install also <strong>apache2-mod_mono</strong> to make possible running ASP.NET mono pages in Apache. do this.</p>
<p>Log off – log on and move to configuration</p>
<h3>Mono configuration</h3>
<p>Now it’s time to configure what ASP.NET pages you want to run. We want ASP.NET 2.0, so we should run mono apache mode for this version. To do this, go to the terminal, elevate yourself (su –) and type following: “<strong>vi /etc/apache2/httpd.conf</strong>” This will open VI editor with apache configuration file in it. </p>
<p>Now it’s time to learn VI a little. To <em>start editing</em>, you should type “<strong>A</strong>” – it will write “INSERT” in the lower left corner. To <em>return to the command mode</em>, hit <strong>escape key</strong>. To <em>save</em> (from command mode) “<strong>:w</strong>” to <em>exit and save</em> “<strong>:wq</strong>” to <em>exit without save</em> “<strong>:q!</strong>”. To <em>find</em> “<strong>/</strong>” and string the pattern you are looking for.</p>
<p>Now go the the very end of the file&#160; and write under Include “/etc/apache2/vhosts.d/*.conf” following:   <br />(to short string “[D]” is your virtual directory (slash blank is root), “[P]” is physical path to your site without trailing slash)</p>
<blockquote><p>MonoServerPath default /usr/bin/mod-mono_server2     <br />Alias [D] “[P]”      <br />AddMonoApplications default “[D]:[P]”      <br />&lt;Location [D]&gt;      <br /> SetHandler mono      <br />&lt;/Location&gt;</p>
</blockquote>
<p>So, if your site is MySite and it is in /srv/www/htdocs/MySite, this section will looks as following:</p>
<blockquote><p>MonoServerPath default /usr/bin/mod-mono_server2     <br />Alias /MySite “/srv/www/htdocs/MySite”      <br />AddMonoApplications default “/MySite:/srv/www/htdocs/MySite”      <br />&lt;Location /MySite&gt;      <br /> SetHandler mono      <br />&lt;/Location&gt;</p>
</blockquote>
<p>If you want to turn it to the root site, this will looks following:</p>
<blockquote><p>MonoServerPath default /usr/bin/mod-mono_server2     <br />AddMonoApplications default “/:/srv/www/htdocs/MySite”      <br />&lt;Location /&gt;      <br /> SetHandler mono      <br />&lt;/Location&gt;</p>
</blockquote>
<p>Now, we’ll add mono administrative site to be able to restart mono only without touching apache itself. To do this, after last &lt;/Location&gt; you should add following:</p>
<blockquote><p>&lt;Location /mono&gt;     <br /> SetHandler mono-ctrl      <br /> Order deny,allow      <br /> Deny from all      <br /> Allow from 127.0.0.1      <br />&lt;/Location&gt;</p>
</blockquote>
<p>I think it’s very clear what it did <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you have more, then one site and want to configure mono differently for each one of those, you should add VirtualHost section. To do this, include your configuration in to </p>
<blockquote><p>&lt;VirtualHost [IP and port you want, for example 1.1.1.1:80 or *:80 for all IPs on port 80]&gt;     <br />ServerName [Name you want]      <br />…      <br />&lt;/VirtualHost&gt;</p>
</blockquote>
<p>We done. Restart apache and enter the url you set (for example <a href="http://localhost/MySite/">http://localhost/MySite/</a>)</p>
<p>Working? Good. You finished. </p>
<p>Not working (familiar yellow error 500 screen)? Keep reading…</p>
<h3>Debugging Mono website</h3>
<p>Do you remember, that you have no development environment in this machine? You can install it, or <a href="http://www.go-mono.com/mono-downloads/download.html">download</a> Mono liveCD with openSuse. But before doing it, please note, that GTK# (it’s devenv) is not very user friendly. It even worse, then <a href="http://www.eclipse.org/">Eclipse</a>. So let’s try to understand first whether we can fix small compatibility problems without entering code.</p>
<p>The most convenient method to debug web site on Mono is by using XSP and XSP2 mini web servers. Just enter the directory of the site and run it. By default you’ll be able to access the site by using “http://localhost:8080” (it also be written for you). Enter and notice whether you have any errors in console. No? Keep doing</p>
<p>The most common problem is “error 500” with nonsense stack. If it contains ScriptManager error Type not found, the problem is in Web.config file. Try to regenerate it to be compatible to Mono (for example, Mono has different version of System.Web.Extensions assembly. In ASP.NET 3.5 it has version 3.5, Mono has only 1.0.61025.0 (the old AJAX.NET). To recreate your web.config all you have to do is to execute “<strong>mconfig af AJAX Web.config</strong>” It will create default web.config file, supports System.Web.Extensions (AJAX features).</p>
<div>Not helped? Keep doing. Let’s look another time into the stack – if it contains errors in “EnablePageMethods” or “ShouldGenerateScript” or “EncryptString” – the problem is serialization. Mono has very limited support for JSON, XML and SOAP serialization. Try to look into your code and notice if you have classes, marked with [Serializable] or you are transferring your own classes by using PageMethods. If so, replace it with regular strings (my grandma serialization).
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
<blockquote><p>Person p = new Person();       <br />string sstr = string.Format(“{0}|{1}|{2}|{3}”, p.FirstName, p.LastName, p.Age, p.Wage);        <br />return sstr;        <br />…        <br />var sstr = persons[i].split(&quot;|&quot;);        <br />var p.FirstName =&#160; sstr[0];        <br />var p.LastName =&#160; sstr[1];        <br />var p.Age =&#160; sstr[2];        <br />var p.Wage =&#160; sstr[3];</p>
</blockquote>
<p>Not helped? Try to rename “Bin” directory into “bin” “<strong>mv Bin bin –r</strong>”. Actually this was fixed in latest versions of Mono, but who knows?…</p>
<p>No? Check whether you have partial classes, which is not supported by Mono. If so, recompile it like this</p>
<blockquote><p>mcs /t:library /out:bin/test.dll –r:System.Web –r:System.Data –r:System.Web.Services –r:System.Web.UI.Controls test.aspx.cs</p>
</blockquote>
<p>If you have Generics in your code, you should use <strong>gmcs</strong>, rather then <strong>mcs</strong>.</p>
</p></div>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
<p>Not helped? It looks, that you have to either install Mono on your Windows machine and debug your code with it. Or, alternatively install GTK# and do in on Linux.</p>
<p>But wait, before doing such big step, install and check the binary compatibility of your code. To do this, you need “<a href="http://www.mono-project.com/MoMA">Moma</a>” – a simple tool, that tell you if everything is ok for Mono in your assemblies.</p>
<p>Good luck and see you in <a href="http://blogs.microsoft.co.il/blogs/tamir/archive/tags/teched/default.aspx">my forthcoming TechEd session</a>, where I’m presenting openSuse, running UDP multicast server with ASP.NET 3.5 extended methods (It uses recompiled ISAPI filters for apache, rather then regular limited AJAX support in Mono)</p>
<p>Have a nice day and be good people.</p>


<p>Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/configuring-and-running-mono-aspnet-35-ajaxnet-on-linux-computers/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
