C++ 库的 C# 绑定(bind)

标签 c# c++ .net binding cppsharp

我已经为 libpostal 库 ( link to LibPostalNet ) 创建了一个 c# 绑定(bind)。

我使用 CppSharp 创建绑定(bind)。它有效,但我不知道如何转换此代码:

typedef struct libpostal_address_parser_response
{
    size_t num_components;
    char** components;
    char** labels;
}
libpostal_address_parser_response_t;

CppSharp 以这种方式转换代码:

public sbyte** Components
{
    get
    {
        return (sbyte**)((global::LibPostalNet.LibpostalAddressParserResponse.__Internal*)__Instance)->components;
    }

    set
    {
        ((global::LibPostalNet.LibpostalAddressParserResponse.__Internal*)__Instance)->components = (global::System.IntPtr)value;
    }
}

public sbyte** Labels
{
    get
    {
        return (sbyte**)((global::LibPostalNet.LibpostalAddressParserResponse.__Internal*)__Instance)->labels;
    }

    set
    {
        ((global::LibPostalNet.LibpostalAddressParserResponse.__Internal*)__Instance)->labels = (global::System.IntPtr)value;
    }
}

代码应该返回一个长度为 num_components 的字符串数组。

你能帮我解决这个问题吗?

最佳答案

这有点复杂,因为它们是指向字符指针列表的指针,非常类似于字符串数组。

您需要迭代指针、检索内部指针并将其转换为字符串(我假设您可以使用非托管代码):

libpostal_address_parser_response response = (retrieve from the library);

List<string> components = new List<string>();
List<string> labels = new List<string>();

//Not sure the name of num_components as you haven't added it to the question
//check if the name is correct
for(int buc = 0; buc < response.NumComponents; buc++)
{
    sbyte* pLabel = response.Labels[buc];
    sbyte* pComponent = response.Components[buc];

    labels.Add(Marshal.PtrToStringAuto((IntPtr)pLabel));
    components.Add(Marshal.PtrToStringAuto((IntPtr)pComponent));
}

//Now you have the components and the labels in their respective lists ready for usage.

关于C++ 库的 C# 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46145428/

相关文章:

c# - 检查是否已选择组合框值 - C# .net

c# - .NET 客户端/服务器可扩展性和异步 I/O - 过多线程问题

c# - Svn 外部和 C# 程序集 - 不兼容?

C++传递一个对象

c++ - 如何将 Open GL 驱动程序中的段错误追溯到我的源代码?

c++ 程序查找当前在 gvim 中打开的文件?

.net - MQ系列消息不止一次被读取

C# var关键字混淆: Type 'object' does not contain a definition for. ..错误

c# - MonoTouch.Dialog:禁用整个表格的选择样式

c# - 安卓 XAMARIN : Camera intent is returning with null data in callback