Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You aren't actually marshaling any data over to the managed side. When you declare <code>output</code> on the managed side, it's default value is <code>null</code>. Then, on the unmanaged side, you never allocate any memory for <code>output</code>. You should allocate some unmanaged memory, pass the pointer to that memory to your dll function, then marshal the pointer for that memory to your struct:</p> <pre><code>[StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Ansi)] public struct SYSTEM_OUTPUT { UInt16 ReadyForConnect; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] String VersionStr; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] String NameOfFile; // actually more of those } public partial class Form1 : Form { public SYSTEM_OUTPUT output; [DllImport("testeshm.dll", EntryPoint="getStatus")] public extern static int getStatus(IntPtr output); public Form1() { InitializeComponent(); } private void ReadSharedMem_Click(object sender, EventArgs e) { IntPtr ptr; try { ptr = Marshall.AllocHGlobal(Marshall.SizeOf(typeof(SYSTEM_OUTPUT))); int ret = getStatus(ptr); if(ret == 0) { output = (SYSTEM_OUTPUT)Marshal.PtrToStructure(ptr, typeof(SYSTEM_OUTPUT)); } //do something with output label1.Text = ret; } catch (AccessViolationException ave) { label1.Text = ave.Message; } finally { Marshal.FreeHGlobal(ptr); //make sure to free the memory } } } </code></pre> <p>Edit:<br/></p> <p>Your problem could be an issue with the difference between <a href="http://msdn.microsoft.com/en-us/library/aa366769%28VS.85%29.aspx" rel="nofollow noreferrer">packing</a> strategies. I've updated the struct definition. </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. 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