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

Tony McGrath toeknee at chipmonks.oz.au
Tue Oct 4 19:09:51 EDT 2005


Paolo Molaro wrote:

>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
>
>  
>
I found out what the problem was. I had a fixed char buffer in the 
structure.
This was not being marshalled correctly, which was causing the copy to
fail. Once this was fixed the rest started to work correctly.

Thank you for your generous help.

Tony McGrath
Chip Monks Computing



More information about the Mono-devel-list mailing list