[Mono-list] Re: P/Invoke IntPtr versus byte[]

Marcus mathpup@mylinuxisp.com
Sat, 3 Jul 2004 16:19:11 -0500


I just wanted to ask because it is a little safer to declare the type that 
you're passing in advance. And you could create multiple P/Invoke 
declarations that refer to the same function like

	[DllImport("libwhatever.so", EntryPoint="Method")]
	static extern void MethodTakesIntArray( int[] );

	[DllImport("libwhatever.so", EntryPoint="Method")]
	static extern void MethodTakesFloatArray( float[] );

Back to your main question, I think that this is a bug in Mono's 
implementation of GC.AddrOfPinnedObject() because my test code, which is 
based on the code you provided, works fine on Rotor. I think that Mono is 
returning a pointer to some data structure that's a few bytes before the 
actual array data. If I change the code to store 1 at @ element 1982 and 2 @ 
1983, Mono gives

==> array(0) = 168
==> array(1) = 208
==> array(2) = 4
==> array(3) = 8
==> array(13) = 48
==> array(1998) = 1
==> array(1999) = 2

If you want, I can file a bug report, or you can do it. I just happened in 
checking this out to write a short test-case.


On Saturday 03 July 2004 4:02 pm, Tim Rädisch wrote:
> > Is there some particular reason that you do not know the type in advance?
> > It would be easier if you could do that because you would then be able to
> > write a precise signature for the P/Invoke method.
>
> That's not possible. In detail: this is for an OpenGL-Method, and the array
> can be e.g.
> 	height x width x 3 - byte array; e.g. RGB
> 	height x width x 1 - float array; e.g. alpha
> 	height x width x 4 - float array; e.g. RGBA