1

TL;DRCppSharp 为特定的 C++ 方法签名生成错误的绑定代码。如何使用 TypeMaps 和/或传递来解决这个问题?是否有我可以使用的类型转换的文档或示例?

我正在使用CppSharp为 OBS studio 生成 C# 绑定,以便为其编写插件。

使用以下生成器从 OBS.h 生成代码时:

class OBSBindingsGenerator: ILibrary
{
    public void Postprocess(Driver driver, ASTContext ctx)
    {
    }

    public void Preprocess(Driver driver, ASTContext ctx)
    {
    }

    public void Setup(Driver driver)
    {
        var options = driver.Options;
        options.GeneratorKind = CppSharp.Generators.GeneratorKind.CSharp;
        var module = options.AddModule("OBS");
        module.IncludeDirs.Add(@"path\to\libobs");
        module.Headers.Add("obs.h");
    }

    public void SetupPasses(Driver driver)
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        ConsoleDriver.Run(new OBSBindingsGenerator());
    }
}

我发现它会生成:

    [SuppressUnmanagedCodeSecurity]
    [DllImport("OBS", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
        EntryPoint="video_format_get_parameters")]
    [return: MarshalAs(UnmanagedType.I1)]
    internal static extern bool VideoFormatGetParameters_0(global::OBS.VideoColorspace color_space, global::OBS.VideoRangeType range, float matrix, float min_range, float max_range);

和相应的公共方法

public static bool VideoFormatGetParameters(global::OBS.VideoColorspace color_space, global::OBS.VideoRangeType range, float[] matrix, float[] min_range, float[] max_range)
{
    ... // Code snipped for brevity
    fixed (float* __ptr4 = max_range)
    {
        var __arg4 = new global::System.IntPtr(__ptr4);
        // See here that __arg4 is an IntPtr argument and is being passed to a float parameter
        var __ret = __Internal.VideoFormatGetParameters_0(color_space, range, __arg2, __arg3, __arg4);
        return __ret;
    }
}

这显然不能编译。C++ 定义是:

EXPORT bool video_format_get_parameters(enum video_colorspace color_space,
    enum video_range_type range, float matrix[16],
    float min_range[3], float max_range[3]);

在手写 PInvoke 中,可以通过多种方式解决此问题(声明具有正确数量的 float 成员的结构,或 a[MarshalAs(UnmanagedType.LPArray, SizeConst = 3)]和 float[] 传递或将 PInvoke 参数声明为 IntPtr)。

那么我的问题是:如何使用 CppSharp 及其代码转换工具(TypeMap?Pass?什么?)来实现任何正确的翻译?我可以将其修复为 hack,但由于我与我的 C++ 库互操作,我更愿意学习使用 CppSharp 执行此操作的正确方法(学习钓鱼)。

提前致谢!

4

1 回答 1

0

请尝试使用最新的 CppSharp 版本(0.8.14+),它包含与数组参数相关的修复。

于 2017-09-29T15:44:16.873 回答