title: Passing a struct pointer which contains an array as a parameter in C#
body: <p>I have a function in C++ and exported in DLL.
  the function is</p>
  
  <pre><code>LONG LOGIN(LPDEVINFO info);
  </code></pre>
  
  <p>the struct of <code>LPDEVINFO</code> is:</p>
  
  <pre><code>struct{  
         BYTE sSerialNumber[20];
  } *LPDEVINFO;
  </code></pre>
  
  <p>to pass <code>LPDEVINFO</code> parameter, I have defined a class in managed code:</p>
  
  <pre><code>class DEVINFO{
       Byte[] sSerialNumber = new Byte[20];
  }
  </code></pre>
  
  <p>and then P/Invoke like this:</p>
  
  <pre><code>[DllImport ('MyDll.dll')]
  public static extern Int32 LOGIN(DEVINFO info);
  </code></pre>
  
  <p>and then call it in C#:</p>
  
  <pre><code>DEVINFO info = new DEVINFO();
  Int id = LOGIN(info)
  </code></pre>
  
  <p>When I run this code, I got following error:</p>
  
  <pre><code>An unhandled exception of type 'System.AccessViolationException' occurred in WindowsFormsApplication1.exe
  
  Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  </code></pre>
  
  <p>I think the problem is caused by the array <code>sSerialNumber</code>. But I do not know how to define it in a right way.</p>
  
  <p>Thanks in advance!</p>