TL;DR : CppSharp generates the wrong binding code for a particular C++ method signature. How do I use TypeMaps and or passes to fix this? Is there documentation or examples of type transformations I can use?
I'm using CppSharp to generate C# bindings for OBS studio, in order to write a plugin for it.
When generating code from OBS.h using the following generator:
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());
}
}
I find that it generates:
[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);
and the corresponding public method
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;
}
}
This obviously does not compile. The C++ definition is:
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]);
In hand-written PInvoke, this can be fixed in many ways (declare a struct with the right number of float members, or a [MarshalAs(UnmanagedType.LPArray, SizeConst = 3)]
and a float[] passthrough or declare the PInvoke parameter as an IntPtr).
My question then is: How do I use CppSharp and its code transformation tools (TypeMap? Pass? What?) to achieve any of the correct translations? I could fix this as a hack, but since I interop with my C++ libraries I'd prefer to learn the right way to do this with CppSharp (learn to fish).
Thanks in advance!
请尝试使用最新的CppSharp版本(0.8.14+),其中包含与数组参数有关的修复程序。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.