Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I haven't tested any of this, so it may need some changes, but here's what I see off hand.</p> <p>First off, <code>STRUCT</code> should be declared as follows:</p> <pre><code>struct STRUCT { public UInt32 length; } </code></pre> <p>Notice that we changed from <code>class</code> to <code>struct</code> so that the memory layout of the object is known and matches what the C code is expecting.</p> <p><strong>Update</strong></p> <p>After a bit of further consideration, there's an easier way of declaring and calling the method, I'll leave the original answer below for another way of doing it.</p> <p>Your P/Invoke signature should be:</p> <pre><code>[DllImport(LINUXLIB, CallingConvention=CallingConvention.Cdecl)] public static extern int simpletran(ref STRUCT req); </code></pre> <p>Notice that we changed <code>STRUCT</code> to <code>ref STRUCT</code> since <code>STRUCT</code> is a value type and the C code requires a pointer to that structure.</p> <p>And you'd call it like this:</p> <pre><code>STRUCT st = new STRUCT(); st.length = (UInt32)100; simpletran(ref st); </code></pre> <p><strong>Original</strong></p> <pre><code>[DllImport(LINUXLIB, CallingConvention=CallingConvention.Cdecl)] public static extern int simpletran(IntPtr req); </code></pre> <p>Notice that we changed <code>STRUCT</code> to <code>IntPtr</code> since <code>STRUCT</code> has to be a value type and the C code requires a pointer to that structure.</p> <p>And you'd call it like this:</p> <pre><code>STRUCT st = new STRUCT(); st.length = (UInt32)100; IntPtr ptr = Marshal.AllocHGlobal(sizeof(STRUCT)); Marshal.StructureToPtr(st, ptr, false); simpletran(ptr); Marshal.FreeHGlobal(ptr); </code></pre> <p>Adding the extra steps in between creating the instance and calling the method for allocating a chunk of unmanaged memory to store the value of the struct in (<a href="http://msdn.microsoft.com/en-us/library/s69bkh17.aspx" rel="nofollow">Marshal.AllocHGlobal</a>) and copying the value of <code>st</code> into that memory with <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.structuretoptr.aspx" rel="nofollow">Marshal.StructureToPtr(...)</a>. Be sure to free the allocated memory after the call with a call to <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.freehglobal.aspx" rel="nofollow">Marshal.FreeHGlobal</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload