[Mono-dev] Embedded Mono - Structures and internal C methods

Paolo Molaro lupus at ximian.com
Tue Oct 4 10:38:54 EDT 2005


On 10/04/05 Tony McGrath wrote:
> I have been trying, with little success, to work out how to pass a 
> struct from
> C# into a C routine that has been registered with mono_add_internal_call().
> 
> Specifically, the struct has to be modified within the C routine and those
> changes need to be reflected back into the managed C# struct.
> 
> I assumed, probably incorrectly, that the standard P/Invoke mechanisms
> would be appropriate, and with the C# code using a ref modifier it appears
> as if the structure is being sent into the C routine properly, but the 
> modified
> struct is not being copied back into the C# side at all.
> 
> Is there any particular magic that is required with internal calls with 
> an embedded
> application to get the copy in/copy out mechanism to work correctly?

There is no magic involved. If you posted your sample code we could
easily spot the error you're making.
Did you have a look at mono/docs/internal-calls?

Anyway, assume the structure is like this:

typedef struct {
	int a;
	MonoString *b;
} MyStruct;

with the following C# representation:

[StructLayout (LayoutKind.Sequential)]
struct MyStruct {
	int a;
	string b;
};

and the following C# icall declaration:

	static extern void my_icall (ref MyStruct s);

The C-side of the icall must look like:

	void my_icall_impl (MyStruct *s) {
		// do something
	}

lupus

-- 
-----------------------------------------------------------------
lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better



More information about the Mono-devel-list mailing list