我正在尝试从.Net Core应用程序调用C中的函数。 要深入探究,C函数来自libmpv render.h,函数的头部如下所示:
int mpv_render_context_create(mpv_render_context **res, mpv_handle *mpv, mpv_render_param *params);
问题是我不知道如何从C#调用函数。因此,我尝试了以下操作:
[DllImport("mpv-1.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int mpv_render_context_create([Out] out IntPtr renderContext, IntPtr mpvHandle,
MpvRenderParam[] parameters);
由于**res
参数应该由该函数更新,因此我认为这很有意义。我尝试用以下方法对此进行调用:
var ptr = IntPtr.Zero;
var apiTypePtr = Marshal.StringToHGlobalAuto("opengl");
var i = mpv_render_context_create(out ptr, _mpvHandle, new []
{
new MpvRenderParam(MpvRenderParamType.ApiType, apiTypePtr),
new MpvRenderParam(MpvRenderParamType.Invalid, IntPtr.Zero
});
这引发了一个AccessViolationException,实际上,每次我调用mpv_render_context_create
方法时,我都会得到一个异常,所以当我说“它不起作用”时,我的意思是该调用引发了该异常。
所以我当时想也许我必须将其设为ref参数:
[DllImport("mpv-1.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int mpv_render_context_create(ref IntPtr renderContext, IntPtr mpvHandle,
MpvRenderParam[] parameters);
哪个在调用时引起了相同的错误。
然后我读了另一个stackoverflow问题,我应该像下面这样通过裸IntPtr:
[DllImport("mpv-1.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int mpv_render_context_create(IntPtr renderContext, IntPtr mpvHandle,
MpvRenderParam[] parameters);
但是首先,它没有再起作用,其次,我什至如何从中得到结果?
那么我应该如何创建和调用函数,以便创建渲染上下文?
MpvRenderParam 声明如下:
[StructLayout(LayoutKind.Sequential)]
public struct MpvRenderParam
{
public MpvRenderParamType Type { get; }
public IntPtr Data { get; }
public MpvRenderParam(MpvRenderParamType type, IntPtr data)
{
Type = type;
Data = data;
}
}
和 MpvRenderParamType :
public enum MpvRenderParamType
{
Invalid = 0,
ApiType = 1,
InitParams = 2,
Fbo = 3,
FlipY = 4,
Depth = 5,
IccProfile = 6,
AmbientLight = 7,
X11Display = 8,
WlDisplay = 9,
AdvancedControl = 10,
NextFrameInfo = 11,
BlockForTargetTime = 12,
SkipRendering = 13,
DrmDisplay = 14,
DrmDrawSurfaceSize = 15,
DrmDisplayV2 = 15
}
我已经考虑了Mattias Santoro提供的所有资源,因为它是托管语言,所以我不确定如何将它们转换为C#。我的mpv函数调用方法现在看起来像这样:
OpenGlControl.OpenGL.MakeCurrent();
IntPtr ptr;
var apiTypePtr = Marshal.StringToHGlobalAuto("opengl");
var opengl = new MpvOpenGlInitParams {get_proc_address = getProcAdress};
var size = Marshal.SizeOf(opengl);
var arr = new byte[size];
fixed (byte* arrPtr = arr)
{
Marshal.StructureToPtr(opengl, (IntPtr)arrPtr, true);
var parameters = new MpvRenderParam[]
{
new MpvRenderParam {Type = MpvRenderParamType.ApiType, Data = &apiTypePtr},
new MpvRenderParam {Type = MpvRenderParamType.OpenGlInitParams, Data = &arrPtr},
new MpvRenderParam {Type = MpvRenderParamType.Invalid, Data = null}
};
var i = mpv_render_context_create(&ptr, _mpvHandle, parameters);
}
MpvOpenGlInitParams :
[StructLayout(LayoutKind.Sequential)]
public struct MpvOpenGlInitParams
{
public delegate IntPtr GetProcAdressDelegate(IntPtr ctx, string name);
public GetProcAdressDelegate get_proc_address;
public IntPtr get_proc_address_ctx;
public string extra_exts;
}
可悲的是,它仍然抛出相同的错误,我不确定自己在做什么错,这真的令人沮丧。
答案 0 :(得分:2)
我已将代码放入此存储库中:(用于.net core 2.1和X64的sdl2和mpv dll) https://github.com/shodo/MPVCore.git
Update2:DOOOONEEEE! 首先,这是我的代码(真的很丑)。我不知道我是否需要做的每件事:
函数定义为:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int MpvRenderContextCreate(ref IntPtr context, IntPtr mpvHandler, IntPtr parameters);
private MpvRenderContextCreate _mpvRenderContextCreate;
和代码(这是c ++ sdl示例的硬编码弗兰克斯坦的一部分,加上winform和其他内容)
if (_mpvHandle != IntPtr.Zero)
_mpvTerminateDestroy(_mpvHandle);
LoadMpvDynamic();
if (_libMpvDll == IntPtr.Zero)
return;
_mpvHandle = _mpvCreate.Invoke();
if (_mpvHandle == IntPtr.Zero)
return;
_mpvInitialize.Invoke(_mpvHandle);
MpvOpenGlInitParams oglInitParams = new MpvOpenGlInitParams();
oglInitParams.get_proc_address = (ctx, name) => SDL.SDL_GL_GetProcAddress(name);
oglInitParams.get_proc_address_ctx = IntPtr.Zero;
oglInitParams.extra_exts = IntPtr.Zero;
var size = Marshal.SizeOf<MpvOpenGlInitParams>();
var oglInitParamsBuf = new byte[size];
fixed (byte* arrPtr = oglInitParamsBuf)
{
IntPtr oglInitParamsPtr = new IntPtr(arrPtr);
Marshal.StructureToPtr(oglInitParams, oglInitParamsPtr, true);
MpvRenderParam* parameters = stackalloc MpvRenderParam[3];
parameters[0].type = MpvRenderParamType.ApiType;
parameters[0].data = Marshal.StringToHGlobalAnsi("opengl");
parameters[1].type = MpvRenderParamType.InitParams;
parameters[1].data = oglInitParamsPtr;
parameters[2].type = MpvRenderParamType.Invalid;
parameters[2].data = IntPtr.Zero;
var renderParamSize = Marshal.SizeOf<MpvRenderParam>();
var paramBuf = new byte[renderParamSize * 3];
fixed (byte* paramBufPtr = paramBuf)
{
IntPtr param1Ptr = new IntPtr(paramBufPtr);
Marshal.StructureToPtr(parameters[0], param1Ptr, true);
IntPtr param2Ptr = new IntPtr(paramBufPtr + renderParamSize);
Marshal.StructureToPtr(parameters[1], param2Ptr, true);
IntPtr param3Ptr = new IntPtr(paramBufPtr + renderParamSize + renderParamSize);
Marshal.StructureToPtr(parameters[2], param3Ptr, true);
IntPtr context = new IntPtr(0);
_mpvRenderContextCreate(ref context, _mpvHandle, param1Ptr);
}
}
该函数起作用的原因是:
parameters[0].data = Marshal.StringToHGlobalAnsi("opengl");
代替使用
parameters[0].data = Marshal.StringToHGlobalAuto("opengl");
我认为这不仅仅是识别使用opengl渲染上下文启动mpv播放器所需的“ opengl”参数
更新: 为了调用该函数,您需要一个初始化的opengl上下文,或者,您应该将MPV_RENDER_PARAM_OPENGL_INIT_PARAMS类型的参数与访问打开的gl函数所需的回调一起传递。 在使用SDL的C ++中检查此示例,以初始化窗口并获取opengl上下文。
https://github.com/mpv-player/mpv-examples/blob/master/libmpv/sdl/main.c
如果没有适当的初始化OpenGl上下文,则始终会收到有关正确的c#封送处理的ViolationException错误:
github.com/mpv-player/mpv/issues/6249
https://github.com/mpv-player/mpv/issues/6507
-旧: 看到这个: https://stackoverflow.com/questions/20419415/c-sharp-call-c-dll-passing-pointer-to-pointer-argument