What I'm trying to do is to pass an object to a unmanaged routine. This more or less works in .NET.
/* Unmanaged C */
#include <windows.h>
#include <stdlib.h>
typedef struct
{
int i;
} test_arr_t;
typedef struct
{
test_arr_t* arr;
int nr_of_arrs;
} test_t;
__declspec (dllexport) int modify_it (test_t* testme)
{
test_arr_t* tarr = (test_arr_t *) malloc (2 * sizeof (test_arr_t));
/* This won't work...why oh why?
/* testme = (test_t *) malloc ( sizeof(test_t)); */
testme->arr = tarr;
tarr++->i = 1;
tarr->i = 2;
testme->nr_of_arrs = 2;
return 0;
}
With the proper C# proggie:
using System;
using System.Runtime.InteropServices;
namespace teststruct
{
[ StructLayout( LayoutKind.Sequential )]
class test_struct_arr
{
public int i;
}
[ StructLayout( LayoutKind.Sequential )]
class test_struct
{
public IntPtr arr;
public int nr_of_arrs;
}
class Class1
{
[DllImport ("library")]
static extern int modify_it ([In, Out] test_struct inta);
[STAThread]
static void Main(string[] args)
{
test_struct testme = new test_struct();
modify_it (testme);
test_struct_arr[] tarr = new test_struct_arr[testme.nr_of_arrs];
IntPtr current = testme.arr;
Console.WriteLine ("Returned object has {0} elements", testme.nr_of_arrs);
for( int i = 0; i < testme.nr_of_arrs; i++ )
{
tarr[ i ] = new test_struct_arr();
Marshal.PtrToStructure( current, tarr[ i ]);
//Marshal.FreeCoTaskMem( (IntPtr)Marshal.ReadInt32( current ));
Marshal.DestroyStructure( current, typeof(test_struct_arr) );
current = (IntPtr)((int)current +
Marshal.SizeOf( tarr[ i ] ));
Console.WriteLine ("i[{0}] = {1}", i, tarr[i].i);
}
}
}
}
But it doesn't work on mono!!! And that is where I'm aiming to. Also, I'd rather prefer to have the parameter passed to pinvoke to be an 'out' parameter, but it doesn't work either. These are just sample structures. The actual structures I need to pass are a little more complicated, but I'm trying to get these simpler ones right first.Thank you thank you thank you!!!
|
-- Pablo Baena <[EMAIL PROTECTED]> |
<<attachment: smiley-3.png>>
