CppSharp生成错误的代码
CppSharp generates the wrong code
TL;DR: CppSharp 为特定的C++方法签名生成错误的绑定代码。如何使用类型映射和/或通行证来解决此问题?是否有可以使用的类型转换的文档或示例?
我正在使用 CppSharp 为 OBS 工作室生成 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(@"pathtolibobs");
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)]
和 float[] 直通,或将 PInvoke 参数声明为 IntPtr(。
那么我的问题是:如何使用CppSharp及其代码转换工具(TypeMap?通过?什么?实现任何正确的翻译?我可以将其修复为黑客,但由于我与C++库互操作,我更愿意学习使用 CppSharp (学习钓鱼(执行此操作的正确方法。
提前感谢!
请尝试使用最新的CppSharp版本(0.8.14+(,它包含与数组参数相关的修复。
相关文章:
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- VI工作室代码错误无法打开输出文件主.exe
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 有关矩阵的代码错误导致分段错误(内核转储)
- C++ Schannel POST 400 错误代码错误请求
- Java 本机访问代码错误:"Invalid memory access"
- 代码错误修改
- C++代码错误分配
- "_FCbuild"不能用作函数 - C/C++ 代码错误
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 代码错误(从 1 到 100 找到质数)
- OpenCV VisualStudio,C 代码错误尝试访问像素
- 当我使用的教程完全没有错误时,为什么我的代码错误
- 简单的NOOB C 输入/输出问题:代码错误
- 为什么enumProcessModules返回false值和299代码错误
- WinhttpSetoption()失败设置TLSV1.2,带有错误代码错误_internet_incorrect_ha
- 苹果安全传输代码错误
- C++ - 国际象棋主教移动代码错误
- 导入Python库时,嵌入了Python代码错误
- 编译提升C 代码错误