[Mono-dev] When passing managed array of COM object references (Marshaled as UnmanagedType.LPArray) to native Code, expected native COM ptrs are invalid.

Tom Hindle tom_hindle at sil.org
Thu Aug 14 20:43:31 EDT 2008


Hi,

When passing managed array of COM object references (Marshaled as
UnmanagedType.LPArray) to native Code, expected native COM ptrs are
invalid. This same technique works when running on MS .Net. 

1. I'm running on Linux (Hardy), using mono build from svn.

2. I have created 2 C++ COM objects using libCom.

3. I have created a c# Interop to call/create these COM objects.

relevant line in Interop is:

[MethodImpl(MethodImplOptions.InternalCall,MethodCodeType=MethodCodeType.Runtime)]
public virtual extern void PassComArray(
int length, 
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0]
INativeCreatedObject[] a);

4. I create an instance of the COM object in c#
   I use that to return a Naively created COM object to c#.
   I can use this COM object in c#.
   However passing this COM object ref as an array back to native
doesn't work.

IE:

Test t = new Test(); // Create Com object Test
INativeCreatedObject i;
t.CreateNativeObject(out i); // return another Com object created
naively. // prints address as 0x83cbd50

i.DoSomething(); // Works fine.

// Create managed array 
INativeCreatedObject[] array = new INativeCreatedObject[10];
array[0] = i;

t.PassComArray(1, array); // address of array element 0 is 0x60d80 not
0x83cbd50 which means using the COM object ptr will crash.

5. This Marshaling technique works in mono when passing int's. 

IE:
				MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType=MethodCodeType.Runtime)]
public virtual extern void
PassIntArray([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]
 int length, int[] a);

6. after gdb-ing mono I suspect the problem with the CIL that is
generated for the marshaling (is it called the Runtime Callable
Wrapper?) However I can't verify this as I can't program in any
Assembler :(

I have a work around using a custom marshaler, but I would rather use
the same interops on both Windows and Linux.

Thanks in advance for any advice.
Tom






More information about the Mono-devel-list mailing list