mono/CppSharp

CppSharp generates incorrect cast for `const char**` in `struct`

Opened this issue · 1 comments

Hi,

I am using CppSharp 1.1.5.3168 and it generally works fine, given its limitations, but... there's one annoying bug where it generates the wrong return type for the following construct:

namespace perlang::collections
{
    struct StringArray {
        const char** items;
        unsigned long size;
    };
}

This causes the following C# code to be generated:

public sbyte** Items
{
    get
    {
        return (string*) ((__Internal*)__Instance)->items;
    }
}

But this will, naturally, not fly. The return (string *) part there should be return (sbyte**). If I edit the generated code like this, it works perfectly, but it's a bit annoying to have to do this every time the interop code is regenerated. 🙂

If you can point me in the right direction in the codebase, I'm happy to attempt to provide a fix for this. Thanks for a nice tool. 🙏

I think it could be going through this code path: https://github.com/mono/CppSharp/blob/main/src/Generator/Generators/CSharp/CSharpMarshal.cs#L556

Best thing is build from source and get the debugger on it so you can see what code it executes for that case.

If that's the case maybe you need to change IsConstCharString so it does not detect the ** as const string, but just *.