title: C++ function calling from C# application. Attempted to read or write protected memory
body: <p>The problem below is ralated to my previous question </p>
  
  <p><a href="https://stackoverflow.com/questions/2384932/converting-static-link-library-to-dynamic-dll">Converting static link library to dynamic dll</a></p>
  
  <p>My first step was to develop a dll, that was done. (Thanks John Knoeller prakash. Your input was very helpful)</p>
  
  <p>Now when i call the function in the dll from my c# application i get the error</p>
  
  <p><strong>"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."</strong></p>
  
  <p>Here is the C++ definition </p>
  
  <pre><code> extern "C" DEMO2_API void Decompress(char* inp_buff, unsigned short* 
   inp_len, char* buffer_decomp,unsigned *output_len,unsigned short* errorCode);
  </code></pre>
  
  <p>My C# Converstion p/Involke</p>
  
  <pre><code>   private static extern void Decompress(
          byte[] inp_buff,
          ref ushort inp_len,
          byte[] buffer_decomp,
          ref int output_len,
          ref ushort errorCode
          );        
  </code></pre>
  
  <p>And I am calling it as below</p>
  
  <pre><code>    byte[] dst = new byte[2048];
      int outlen = 2048;
      ushort errorCode = 0;
      Decompress(src, (ushort )src.Length, dst, ref outlen,ref errorCode);
      return dst;
  </code></pre>
  
  <p>What is wrong?</p>