[Mono-list] How to pass an object as ref or out

Robert Jordan robertj at gmx.net
Thu Apr 17 16:39:43 UTC 2014


On 17.04.2014 18:33, Robert Jordan wrote:
> On 17.04.2014 16:55, carmack2000 wrote:
>> After I followed your code I got this error:
>> Unhandled exception at 0x100b8c87 in MonoScriptingTest.exe: 0xC0000005:
>> Access violation reading location 0x00000000.
>> I'll look into this.
>>
>> You said 'This is no better than the former non-ref version.' If I
>> want to
>> use ref-object to return values (GetPosition), how can I get it ?
>>
>> C#
>> [MethodImplAttribute(MethodImplOptions.InternalCall)]
>> public static extern void GetPosition(string name, ref Vector2 o)
>>
>> C++: still doing like this ?
>> void GetPosition(MonoString* ms, MonoObject** mo)
>> {
>>          Vector2 pos = *(Vector2*)mono_object_unbox(*mo);
>>          pos.x = xxxx; // Returned to the caller
>>          pos.y = xxxx;
>> }
>
> void GetPosition(MonoString* ms, Vector2 *vector)
> {
>      vector->x = ...
> }
>
> It assumes that you have a C/C++ Vector2 declaration
> that is fully layout-compatible with the managed version.
>
> Robert

Additionally, to make this work Vector2 must be a value type,
but it seems to be one because otherwise you won't call
mono_object_unbox on it.

However, the thread's subject might imply that the above code
were suitable for object arguments. Of course it's not,
so caveat emptor.


Robert




More information about the Mono-list mailing list