CppSharp生成错误代码
问题描述:
TL; DR:CppSharp为特定的C++方法签名生成错误的绑定代码。如何使用TypeMaps和/或传递来解决这个问题?是否有文档或我可以使用的类型转换的示例?CppSharp生成错误代码
我正在使用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的,这可以被固定在很多方面(声明一个结构与右数浮子构件,或[MarshalAs(UnmanagedType.LPArray, SizeConst = 3)]
和浮子[]直通或声明的PInvoke参数作为IntPtr)。
那么我的问题是:如何使用CppSharp及其代码转换工具(TypeMap?Pass?What?)来实现任何正确的转换?我可以将其解决为黑客攻击,但是由于我与C++库进行了互操作,我宁愿学习使用CppSharp(学会钓鱼)的正确方法。
在此先感谢!
答
请尝试使用最新的CppSharp版本(0.8.14+),它包含与数组参数相关的修复。