Writing a lite-weight libVLC wrapper. Need help (Newish to P/Invoke)

老子叫甜甜 提交于 2019-12-08 03:43:25

It doesn't have anything to do with the way you call the function. You cannot get anywhere when you get IntPtr.Zero back from libvlc_new(). It means "there was an error". You'll need to focus on error reporting first, call libvlc_errmsg() to try to get a description for the problem.

So after much looking around and asking questions I've come full circle. I looked deeply into LibVLC.Net and found how they were importing the DLL functions and adapted what they did to my own wrapper and it worked.

To summarize:

There are some Win32 API functions declared in the code at the start:

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetDllDirectory(string lpPathName);

    [DllImport("kernel32", SetLastError = true)]
    private static extern IntPtr LoadLibrary(string lpFileName);

    [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
    private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool FreeLibrary(IntPtr hModule);

that handle providing a handle to a dll and setting a directory search path.

I don't know exactly what it all means but when you initialize the LibVLC.Net library (the primary object) it loads pretty much EVERY function like so:

m_libvlc_media_player_new = (libvlc_media_player_new_signature)LoadDelegate<libvlc_media_player_new_signature>("libvlc_media_player_new");

That delegate is defined here like so:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr libvlc_media_player_new_signature(IntPtr p_instance);

//==========================================================================
private readonly libvlc_media_player_new_signature m_libvlc_media_player_new;

//==========================================================================
public IntPtr libvlc_media_player_new(IntPtr p_instance)
{
  VerifyAccess();

  return m_libvlc_media_player_new(p_instance);
}

And it has a public function that calls the delegate once defined.

I simply stripped down the function that defines the library instance and imported only the functionality I needed.

Thanks very much to everyone who was so patient in helping me along. I likely wouldn't have been able to come to a solution without your help.

EDIT: Okay so it wasn't that. It was the location of the LibVLC Plugin Directory. So it was something stupid -.-;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!