How to bind Xamarin iOS Obj-C library with marshalling

c#, ios, marshalling, xamarin, xamarin.ios

Solution

Xamarin.iOS binding projects do not support custom marshalling.

But I don't think you need it:

[BaseType (typeof (NSObject))]
public partial interface MarshalTest {

    [Export ("initWithMarshal:")]
    IntPtr Constructor (ref Marshal3D marshal);

    [Export ("initWithMarshals:num:")]
    IntPtr Constructor (Marshal3D[] marshals, int numCoord);

    [Export ("addMarshal:")]
    void AddMarshal (ref Marshal3D marshal);

    [Export ("addMarshals:num:")]
    void AddMarshals (Marshal3D[] marshals, int numCoord);

    [Export ("getMarshals:")]
    [Internal]
    int GetMarshals (IntPtr getMarshals);

    // ...
}

However to support GetMarshals nicely, you'll have to add a custom binding (this is typically done in the StructsAndEnums.cs file):

public partial class MarshalTest : NSObject {
    public unsafe Marshal3D [] GetMarshals () {
        var count = // how many are there? there's no API to fetch this in your sample
        var array = new Marshal3D [count];

        fixed (Marshal3D* ptr = &array[0])
            GetMarshals ((IntPtr) ptr);

        return array;            
    }
}

[Updated to answer second set of questions]

For the constructor, I think this will work:

ApiDefinition.cs

[BaseType (typeof (NSObject))]
public partial interface MarshalTest {
    [Export ("initWithMarshals:num:")]
    [Internal]
    IntPtr Constructor (IntPtr marshals, int numCoord);
}

Extra.cs

public partial class MarshalTest : NSObject {
    public MarshalTest (Marshal3D[] marshals, int numCoord)
        : this (GetPointer (marshals), numCoord)
    {
    }

    static IntPtr GetPointer (Marshal3D[] marshals)
    {
        fixed (Marshal3D* ptr = &marshals[0])
            return (IntPtr) ptr;
    }

}

You're right, it's expecting a pointer to a array. Try this instead:

ApiDefinition.cs

[BaseType (typeof (NSObject))]
public partial interface MarshalTest {
    [Export ("getMarshals:")]
    [Internal]
    int GetMarshals (ref IntPtr getMarshals);
}

Extra.cs

public partial class MarshalTest : NSObject {
    public unsafe Marshal3D [] GetMarshals () {
        var ptr = IntPtr.Zero;
        var count = this.StoredNumber ();
        var array = new Marshal3D [count];

        GetMarshals (ref ptr);

        unsafe {
            Marshal3D* ptr3d = (Marshal3D *) ptr;
            for (int i = 0; i < count; i++)
                array [i] = *ptr3d++;
        }

        return array;            
    }
}

Problem

I want to study how to bind iOS library with handling pointer to Xamarin.iOS. It need marshaling. I prepare such library for test, MarshalTest.h ``` typedef struct { float x,y,z; } Marshal3D; @interface MarshalTest : NSObject -(id)initWithMarshal:(Marshal3D *)marshal; -(id)initWithMarshals:(Marshal3D *)marshals num:(int)numCoord; -(void)addMarshal:(Marshal3D *)marshal; -(void)addMarshals:(Marshal3D *)marshals num:(int)numCoord; -(int)getMarshals:(Marshal3D **)getMarshals; -(int) storedNumber; -(float)checkMarshalValue:(int)index xyz:(int)xyz; @end ``` and prepare Binding c# codes like this: StructsAndEnums.cs ``` using System; using System.Runtime.InteropServices; namespace MarshalTestNet { [StructLayout(LayoutKind.Sequential)] public struct Marshal3D { public float x; public float y; public float z; }; } ``` ApiDefinition.cs ``` using System; using System.Drawing; using System.Runtime.InteropServices; using MonoTouch.ObjCRuntime; using MonoTouch.Foundation; using MonoTouch.UIKit; namespace MarshalTestNet { [BaseType (typeof (NSObject))] public partial interface MarshalTest { [Export ("initWithMarshal:")] IntPtr Constructor ([In, MarshalAs(UnmanagedType.LPStruct)]Marshal3D marshal); [Export ("initWithMarshals:num:")] IntPtr Constructor ([In, MarshalAs(UnmanagedType.LPStruct)]Marshal3D marshals, int numCoord); [Export ("addMarshal:")] void AddMarshal ([In, MarshalAs(UnmanagedType.LPStruct)]Marshal3D marshal); [Export ("addMarshals:num:")] void AddMarshals ([In, MarshalAs(UnmanagedType.LPStruct)]Marshal3D marshals, int numCoord); [Export ("getMarshals:")] int GetMarshals ([Out, MarshalAs(UnmanagedType.LPStruct)]Marshal3D getMarshals); [Export ("storedNumber")]//, Verify ("ObjC method massaged into getter property", "/Users/kokogiko/Desktop/MarshalTest.h", Line = 32)] int StoredNumber { get; } [Export ("checkMarshalValue:xyz:")] float CheckMarshalValue (int index, int xyz); } } ``` But this not work, just calling ``` var mars3 = new Marshal3D{ x = 1.0f, y = 2.0f, z = 3.0f }; var mars = new MarshalTest (mars3); ``` will terminated without showing error message. How to bind iOS library with marshalling? There are very less information for doing this... [Edited after Rolf's answer] Thank you Rolf, I tested many cases so sorry for replying so late. 1. ``` [Export ("initWithMarshal:")] IntPtr Constructor (ref Marshal3D marshal); [Export ("addMarshal:")] void AddMarshal (ref Marshal3D marshal); ``` works very well. Thank you! 2. ``` [Export ("initWithMarshals:num:")] IntPtr Constructor (Marshal3D[] marshals, int numCoord); [Export ("addMarshals:num:")] void AddMarshals (Marshal3D[] marshals, int numCoord); ``` not works, with showing this error in compile stage: ``` obj/Debug/ios/MarshalTestNet/MarshalTest.g.cs(140,31): error CS1502: The best overloaded method match for `MonoTouch.Foundation.NSArray.FromNSObjects(params MonoTouch.Foundation.NSObject[])' has some invalid arguments /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll (Location of the symbol related to previous error) obj/Debug/ios/MarshalTestNet/MarshalTest.g.cs(140,46): error CS1503: Argument `#1' cannot convert `MarshalTestNet.Marshal3D[]' expression to type `MonoTouch.Foundation.NSObject[]' ``` so I change this by myself, ApiDefinition.cs ``` [Export ("addMarshals:num:")] void AddMarshals (IntPtr marshals, int numCoord); ``` Extra.cs ``` public unsafe void AddMarshals (Marshal3D[] marshals) { var count = marshals.Length; fixed (Marshal3D* ptr = &marshals[0]) AddMarshals ((IntPtr)ptr, count); } ``` this works well. But, this approach cannot use for ``` -(id)initWithMarshals:(Marshal3D *)marshals num:(int)numCoord; ``` because it is constructor, not allow to add any logic before calling parent method. Is there any way to fix this? Using "Factory pattern" is best? 3. ``` [Export ("getMarshals:")] [Internal] int GetMarshals (IntPtr getMarshals); ``` and ``` public partial class MarshalTest : NSObject { public unsafe Marshal3D [] GetMarshals () { var count = this.StoredNumber(); var array = new Marshal3D [count]; fixed (Marshal3D* ptr = &array[0]) GetMarshals ((IntPtr) ptr); return array; } } ``` not works. Terminated without showing any errors in runnig phase. I think ``` Marshal3D* ptr = &array[0] ``` is a pointer, but ``` -(int)getMarshals:(Marshal3D **)getMarshals; ``` needs pointer-of-pointer. Isn't it right?

Original source