我正在尝试从我的 .Net Core 应用程序调用 C 中的函数。
深入了解一下,C 函数来自libmpv 渲染.h https://github.com/mpv-player/mpv/blob/master/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);
但首先,它又不起作用了,其次我怎么能从中得到结果呢?
那么我应该如何创建和调用该函数以便创建渲染上下文?
The Mpv渲染参数声明如下:
[StructLayout(LayoutKind.Sequential)]
public struct MpvRenderParam
{
public MpvRenderParamType Type { get; }
public IntPtr Data { get; }
public MpvRenderParam(MpvRenderParamType type, IntPtr data)
{
Type = type;
Data = data;
}
}
And The Mpv渲染参数类型:
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
}
Update
我已经考虑了 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);
}
The 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;
}
可悲的是它仍然抛出同样的错误,我不确定我做错了什么,这真的很令人沮丧。