[Mono-list] SizeParamIndex attribute size ?

Lloyd Dupont lloyd@galador.net
Tue, 02 Oct 2001 01:29:18 +0200


Hy fellow user, any one could help ?

i have a few interop code and i want to pass an array from C to C# via a 
callback.
i could only pass Length 1 array, is it a bug ?

here i join you my code you could comment...
makefile -------------------------
all: s.dll s.exe
	s

s.dll: s.c
	gcc -shared -o s.dll s.c

s.exe: s.cs
	csc /unsafe s.cs
s.c -------------------------
typedef struct MyStruct {
	int begin;
	int data[20];
	int end;
} MyStruct;

void doSomething(MyStruct * s) {
	int i;
	for(i=0; i<20; i++)
		s->data[i] = i * i;
	s->begin = s->end = -1;
}

void callIt(void (*fct)(int *, int), int* tab) {
	fct(tab, 5);
}
s.cs ------------------------
using System;
using System.Runtime.InteropServices;

unsafe class Test
{
	[StructLayout(LayoutKind.Sequential)]
	public struct MyStruct
	{
		public int begin;

		[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
		public int[] data;

		public int end;
	}

	[DllImport("s")]
	public static extern void doSomething(out MyStruct s);
	[DllImport("s")]
	public static extern void callIt(callback fct, int[] tab);

	public delegate void callback(int[] a, int l);
	static void myCallback(
		[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]
		int[] a,
		int len) {
		Console.WriteLine("callback: len: "+len);
		Console.WriteLine("callback: tab.length: "+a.Length);
		a[2] = -25;
	}

	public static void Main(string[] arg)
	{
		MyStruct s;
		doSomething(out s);
		callIt(new callback(myCallback), s.data);

		Console.WriteLine("begin,end =  "+s.begin+","+s.end);
		for(int i=0; i<20; i++)
			Console.WriteLine("s["+i+"] = "+s.data[i]);
	}
}