Question

I'm trying to write a bare-bones ultra-simple light-weight wrapper for the LibVLC DLL Library. I don't need access to much, just the ability to play pause and stop media files. I'm looking at the documentation and this other link I found that explains an older version of LibVLC, but it's outdated for the most recent version. I also tried LibVLC.Net but it too is outdated and I can't find what I'm looking for in the source code to match it to the functions I'm trying to export.

I have the following signature I'm trying to export:

libvlc_new (int argc, const char *const *argv)

The description:

argc    the number of arguments (should be 0)
argv    list of arguments (should be NULL)

And this is the method I'm trying.

[DllImport("libvlc", EntryPoint = "libvlc_new")]
public static extern IntPtr New(Int32 argc, String[] argv);

The description suggests it should be an array, and I think the problem is the second argument. I've tried:

[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] String[] argv

as according to here, and there are a couple of other options such as a String and StringBuilder as suggested here but it still happens that every time I call the function I get an Imbalanced PInvoke stack.

I need to know what the proper calling convention of this, and very likely several other, functions are. A "PInvoke For Dummies" online reference would be super good.

Était-ce utile?

La solution

Not much point in declaring the argument type if only NULL is permitted. Just declare it IntPtr and pass IntPtr.Zero.

The debugger is pointing out that you forgot to declare the CallingConvention. It is not the default for .NET, this is a __cdecl function. So the proper declaration would be:

[DllImport("libvlc", EntryPoint = "libvlc_new", 
     CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr New(int argc, IntPtr argv);

Called as:

New(0, IntPtr.Zero);

Do try to pick a better name...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top