title: Why is the "ref" keyword needed when making a call to a function in a DLL?
body: <p>I'm trying to figure out why I needed to make a DLLImport function have its parameters as "ref int XXX" instead of just "int XXX". With the latter, this was causing the "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." issue.... but when i added "ref" to the parameters... it works just fine. For example:</p>
  
  <pre><code>    [DllImport("someDLL.dll", CallingConvention = CallingConvention.StdCall)]
      public static extern int SomeDLL_GetDevices(ref int devices);
  </code></pre>
  
  <p>^ the above works</p>
  
  <pre><code>    [DllImport("someDLL.dll", CallingConvention = CallingConvention.StdCall)]
      public static extern int SomeDLL_GetDevices(int devices);
  </code></pre>
  
  <p>^ the above crashes and burns</p>
  
  <p>I'm not an export with C++ calls from C# and the marshalling side of it, so if anyone could explain why one works and one doesn't, i'd appreciate it.</p>