title: Unmanaged DLL causing AccessViolationException
body: <p>This one is really starting to give me a headache :(</p>
  
  <p>I have an unmanaged DLL that I'm trying to interop with and it's not going well. The application will sometimes work...but most times, randomly through an AccessViolationException and crash horribly.</p>
  
  <p>I think I've narrowed it down to my mishandling of a single DllImport:</p>
  
  <p>C++ function:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>HTMLRENDERERDLL_REDIST_API void SetDataBuffer( int windowHandle, unsigned char* dataSource, int format, int stride, int totalBufferSize );
  </code></pre>
  
  <p>C# DllImport:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[DllImport("MyDll.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
      static private extern unsafe void SetDataBuffer(Int32 windowHandle, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] dataSource, Int32 format, Int32 stride, Int32 totalBufferSize);
  </code></pre>
  
  <p>Call to said function:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var buffer = new byte[windowWidth * windowHeight * bytesPerPixel];
  SetDataBuffer(windowHandle, buffer, (Int32)0, (Int32)(windowWidth * bytesPerPixel), (Int32)(windowWidth * windowHeight * bytesPerPixel));
  </code></pre>
  
  <p>Is there anything glaringly wrong with this? I suspect that <code>dataSource</code> is a culprit but...not sure how to prove it!</p>
  
  <p>Thanks</p>