title: Opaque pointers with PInvoke in C#
body: <p>I am using a third party C library with the following opaque handle:</p>
  
  <pre><code>typedef struct _VendorHandle *VendorHandle;
  </code></pre>
  
  <p>Here is the vendor's C example of how to load the handle:</p>
  
  <pre><code>VendorHandle handle;
  int err;
  err = vendorLoadFile(&amp;handle, "something.bin");
  </code></pre>
  
  <p>I am trying to call this method in C# with PInvoke using the following declaration:</p>
  
  <pre><code>[DllImport("VendorLib.dll")]
  static extern int vendorLoadFile(IntPtr handle, string path);
  </code></pre>
  
  <p>Then I added the following code to use the declaration:</p>
  
  <pre><code>IntPtr handle = new IntPtr();
  int code = vendorLoadFile(handle, path);
  </code></pre>
  
  <p>When I run it I'm getting the following error:</p>
  
  <blockquote>
    <p>Attempted to read or write protected memory. This is often an
    indication that other memory is corrupt.</p>
  </blockquote>
  
  <p>I know that the DLL is fine and PInvoke is working because I am executing their vendorVersion() method so it has to be something else I'm doing wrong.  </p>