title: Writing a lite-weight libVLC wrapper. Need help (Newish to P/Invoke)
body: <p>I'm writing a super-simple ultra-lite weight .Net wrapper for the LibVLC media library since the only things I need access to are the ability to play, pause and stop media files. I've posted a couple of questions on this and gotten some answers but unfortunately I'm just left with more questions.</p>
  
  <p>We'll start from the top and work down.</p>
  
  <p>The documentation first states I have to initialize VLC with a call to the function with this specification:</p>
  
  <pre><code>libvlc_instance_t* libvlc_new (int argc, const char *const *argv)
  </code></pre>
  
  <p>for which I have the defined the following method:</p>
  
  <pre><code>[DllImport("libvlc", EntryPoint = "libvlc_new", 
  CallingConvention = CallingConvention.Cdecl)]
  public static extern IntPtr NewCore(int argc, IntPtr argv);
  </code></pre>
  
  <p>And I'm calling the function like this:</p>
  
  <pre><code>private IntPtr Instance;
  this.Instance = DGLibVLC.NewCore(0, IntPtr.Zero);
  </code></pre>
  
  <p>I have tried it several different ways. Initially I did not know about the CallingConvention which was leading to an unbalanced stack which brought me here in the first place. That issue was resolved and the method has gone through several iterations, none of which have proved successful, by which I mean IntPtr is always 0 after the method call. I've tried it like it is above, with the second argument being <code>String[] argc</code>, <code>[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[]</code>,
  I've tried having it return to a Long (which actually resulted in the Long having a value in it), but nothing so far has worked correctly.</p>
  
  <p>Does anyone know the correct way to call this function from the LibVLC DLL Library?</p>
  
  <p>EDIT: On a suggestion I tried calling the error message function of the library:</p>
  
  <p>Specification:</p>
  
  <pre><code>const char* libvlc_errmsg (void)
  </code></pre>
  
  <p>Implementation:</p>
  
  <pre><code>[DllImport("libvlc", EntryPoint = "libvlc_errmsg", 
  CallingConvention = CallingConvention.Cdcel)]
  public static extern string GetLastError();
  </code></pre>
  
  <p>Call:</p>
  
  <pre><code>Console.WriteLine(DGLibVLC.GetLastError());
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Null
  </code></pre>
  
  <p>The documentation states it will return Null if there is no error. This must indicate that the initial function call <code>NewCore</code> was working correctly but something is still going wrong somehow.</p>
  
  <p>To be cover all bases I checked that the DLLs match the documentation, they do. 2.0.6.0. The documentation I am referencing is <a href="http://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc.html" rel="nofollow">here</a>.</p>
  
  <p>EDIT: I can confirm there is no error. When using an initialized to zero long variable to store the result of NewCore I can see it returning something. What I am doing wrong here is where I am trying to store the pointer being returned by the unmanaged function that returns the pointer to the object. How do I store the pointer to the opaque structure reference being passed back?</p>