HomeiOS DevelopmentMarshalling variable-sized string C array into C# (Unity iOS)

Marshalling variable-sized string C array into C# (Unity iOS)


I must return a set of strings from Goal-C to C# and might’t wrap my head round how to do that correctly. After I print the returned pointer on managed facet its worth is 0.

Goal-c

FOUNDATION_EXPORT int _GetAllKeys(char** pointer)
{
    NSArray* array = @[@"key1", @"key2"]; // dummy strings
    
    int measurement = (int)array.depend;

    pointer = malloc(measurement * sizeof(char *));
    
    for (int i = 0; i < measurement; i++)
    {
        pointer[i] = MakeStringCopy(array[i]);
    }
    return measurement;
}

C#

[DllImport("__Internal")]
non-public static extern int _GetAllKeys(out IntPtr buffer);

inside static IEnumerable<string> GetAllKeys()
{
    int depend = _GetAllKeys(out var buffer);
    Checklist<string> keys = new Checklist<string>();
    for (int i = 0; i < depend; i++)
    {
    keys.Add(Marshal.PtrToStringUTF8(IntPtr.Add(buffer, IntPtr.Measurement))); //ultimately when i make this work the strings shall be returned as UTF8 however for now its not even making it to this line, so ignore the encoding.
    }

    return keys;
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments