<div class="gmail_quote"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">I have a C# wrapper for a native DLL which exists on both windows and UNIX. However, it doesn't work on Mono because of a behavior difference between Mono and </span><a href="http://MS.NET" target="_blank">MS.NET</a>. <br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">Specifically, I use "unsafe <span><span class="GRcorrect" id="GRmark_80ce4b1c256452f0103a9f852a70960faf8a8811_struct:0">struct</span></span> pointers" to manage pointers to opaque data (rather than IntPtr). I do this to get <span><span class="GRcorrect" id="GRmark_318144c35be6aa55333af82deeb0d7492d52f866_typechecking:0">typechecking</span></span>, because there <span><span class="GRcorrect" id="GRmark_318144c35be6aa55333af82deeb0d7492d52f866_are:1">are</span></span> a bunch of types of pointers, and using IntPtr is dangerously non-type-checked. (I honestly don't <span><span class="GRcorrect" id="GRmark_b1cf8d91f53c0d1c92e6bbbafcaa8b034c9230ad_undertand:0">undertand</span></span> why IntPtr is allowed, rather than always requiring the use of typed unsafe-<span><span class="GRcorrect" id="GRmark_b1cf8d91f53c0d1c92e6bbbafcaa8b034c9230ad_struct:1">struct</span></span>-pointers.) </span><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"> As far as I know, CLR/CTS defines an unsafe-</span><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span><span class="GRcorrect" id="GRmark_b1cf8d91f53c0d1c92e6bbbafcaa8b034c9230ad_struct:2">struct</span></span></span><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">-pointer as a value type. </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><a href="http://stackoverflow.com/questions/4503795/is-it-valid-to-use-unsafe-struct-as-an-opaque-type-instead-of-intptr-in-net-p" rel="nofollow" link="external" style="color:rgb(85,26,139);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif" target="_blank">http://stackoverflow.com/questions/4503795/is-it-valid-to-use-unsafe-struct-as-an-opaque-type-instead-of-intptr-in-net-p</a><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">My code looks something like: </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">// <span><span class="GRcorrect" id="GRmark_381d580ecc5e9b1b7cac7a3c2f90395c0bcc575a_opaque:0">opaque</span></span> types </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span><span class="GRcorrect" id="GRmark_42965d002afb9989f31820220603b93aaec4b0b2_public:0">public</span></span> unsafe <span><span class="GRcorrect" id="GRmark_42965d002afb9989f31820220603b93aaec4b0b2_struct:1">struct</span></span> HDF {}; </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">// NEOERR* hdf_set_value (HDF *hdf, char *name, char *value) </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">[DllImport<span><span class="GRcorrect" id="GRmark_b1d89c598963ca27ae0e789681450f2af6831569_(:0">(</span></span>"<span><span class="GRcorrect" id="GRmark_b1d89c598963ca27ae0e789681450f2af6831569_libneo:1">libneo</span></span>")] </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span><span class="GRcorrect" id="GRmark_6e6ee78e7968a12655d843892e1bf69d27517f33_private:0">private</span></span> static unsafe extern NEOERR* hdf_set_value<span><span class="GRcorrect" id="GRmark_6e6ee78e7968a12655d843892e1bf69d27517f33_(:1">(</span></span>HDF *hdf, </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">     [MarshalAs<span><span class="GRcorrect" id="GRmark_54f96d0614cc9f824c85c7c9b81097dc989872fa_(:0">(</span></span>UnmanagedType<span><span class="GRcorrect" id="GRmark_54f96d0614cc9f824c85c7c9b81097dc989872fa_.:1">.</span></span>LPStr)] </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">      <span><span class="GRcorrect" id="GRmark_eec5e06df3d755d36cecfd64dd96156d9e62dfbd_string:0">string</span></span> name, </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">     [MarshalAs<span><span class="GRcorrect" id="GRmark_54f96d0614cc9f824c85c7c9b81097dc989872fa_(:0">(</span></span>UnmanagedType<span><span class="GRcorrect" id="GRmark_54f96d0614cc9f824c85c7c9b81097dc989872fa_.:1">.</span></span>LPStr)] </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">      <span><span class="GRcorrect" id="GRmark_3c74cceea23a2004e6fdd919500c551838f8d764_string:0">string</span></span> value); </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">This works fine in</span> <a href="http://MS.NET" target="_blank">MS.NET</a>,<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"> but Mono complains with the following error: </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">Unhandled Exception: System<span><span class="GRcorrect" id="GRmark_c5ae758aedb6f73628057dc47a5823731eed4cfa_.:0">.</span></span>Runtime<span><span class="GRcorrect" id="GRmark_c5ae758aedb6f73628057dc47a5823731eed4cfa_.:1">.</span></span>InteropServices<span><span class="GRcorrect" id="GRmark_c5ae758aedb6f73628057dc47a5823731eed4cfa_.:2">.</span></span>MarshalDirectiveException: Can not marshal 'parameter #1': Pointers can not reference marshaled structures. Use <span><span class="GRcorrect" id="GRmark_1a6624f77def3503af107791acf555bbb3211109_byref:0">byref</span></span> instead. </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">  <span><span class="GRcorrect" id="GRmark_715c42d096226d1ed8734caf16478a95ec83fa3d_at:0">at</span></span> (wrapper managed-to-native) Clearsilver<span><span class="GRcorrect" id="GRmark_715c42d096226d1ed8734caf16478a95ec83fa3d_.:1">.</span></span>Hdf<span><span class="GRcorrect" id="GRmark_715c42d096226d1ed8734caf16478a95ec83fa3d_::2">:</span></span>hdf_set_value (Clearsilver<span><span class="GRcorrect" id="GRmark_715c42d096226d1ed8734caf16478a95ec83fa3d_.:3">.</span></span>HDF*<span><span class="GRcorrect" id="GRmark_715c42d096226d1ed8734caf16478a95ec83fa3d_,:4">,</span></span>string<span><span class="GRcorrect" id="GRmark_715c42d096226d1ed8734caf16478a95ec83fa3d_,:5">,</span></span>string) </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">  <span><span class="GRcorrect" id="GRmark_da4e36fe4970f245864d074f9b5395ad1b25f787_at:0">at</span></span> Clearsilver<span><span class="GRcorrect" id="GRmark_da4e36fe4970f245864d074f9b5395ad1b25f787_.:1">.</span></span>Hdf<span><span class="GRcorrect" id="GRmark_da4e36fe4970f245864d074f9b5395ad1b25f787_.:2">.</span></span><span><span class="GRcorrect" id="GRmark_da4e36fe4970f245864d074f9b5395ad1b25f787_setValue:3">setValue</span></span> (System<span><span class="GRcorrect" id="GRmark_da4e36fe4970f245864d074f9b5395ad1b25f787_.:4">.</span></span>String name, System<span><span class="GRcorrect" id="GRmark_da4e36fe4970f245864d074f9b5395ad1b25f787_.:5">.</span></span>String value) [0x00000] </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">  <span><span class="GRcorrect" id="GRmark_2e32859782eab4f1329f8aa8b696e52dcdc55102_at:0">at</span></span> <span><span class="GRcorrect" id="GRmark_2e32859782eab4f1329f8aa8b696e52dcdc55102_CSTest:1">CSTest</span></span><span><span class="GRcorrect" id="GRmark_2e32859782eab4f1329f8aa8b696e52dcdc55102_.:2">.</span></span>Main (System<span><span class="GRcorrect" id="GRmark_2e32859782eab4f1329f8aa8b696e52dcdc55102_.:3">.</span></span>String<span><span class="GRcorrect" id="GRmark_2e32859782eab4f1329f8aa8b696e52dcdc55102_[:4">[</span></span>] argv) [0x00000] </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">My questions: </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">(1) Why is Mono <span><span class="GRcorrect" id="GRmark_c51b19b6aabd6ebf6f9ec86f0b915c238c5da343_complaining:0">complaining</span></span> about this? AFAIK, in the CLR, an unsafe <span><span class="GRcorrect" id="GRmark_045d27eaef0fdabdf082badc0b865d15015a82ce_struct:0">struct</span></span> pointer is not a marshalled structure, it's a value type. </span><br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">


<br style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">(2) Why would it suggest using ByRef? <span><span class="GRcorrect" id="GRmark_404e5c3e4213e820c3df6ce4ae51d44a5d2b2def_ByRef:0">ByRef</span></span> is for <span><span class="GRcorrect" id="GRmark_404e5c3e4213e820c3df6ce4ae51d44a5d2b2def_managed:1">managed</span></span> pointers to managed data. This is a value-type pointer value.  AFAIK, ByRef is not capable of doing anything here, because the pointer is just a "typed" IntPtr value type. </span><div>


<span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><br></span></div><div><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif">(3) I think this used to work <span><span class="GRcorrect" id="GRmark_677e0af4e21ec68f76772873f0fb057760e81b38_in:0">in</span></span> older versions of mono. Is there some particular reason the behavior has changed? Do folks agree this is a problem?</span></div>


<div><span style="color:rgb(34,34,34);font-size:13px;font-family:Verdana,Geneva,Helvetica,Arial,sans-serif"><br></span></div>
</div><br>