CppSharp生成错误的代码

时间:2017-07-14 15:51:59

标签: c# c++ mono code-generation cppsharp

TL; DR CppSharp为特定的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成员的结构,或者[MarshalAs(UnmanagedType.LPArray, SizeConst = 3)]和一个float [] passthrough,或者将PInvoke参数声明为IntPtr)

我的问题是:如何使用CppSharp及其代码转换工具(TypeMap?Pass?What?)来实现任何正确的翻译?我可以解决这个问题,但是由于我与我的C ++库互操作,我更愿意学习使用CppSharp(学习捕鱼)的正确方法。

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

相关问题