[Mono-dev] libmtp bindings

Jonathan Pryor jonpryor at vt.edu
Fri Jan 19 07:12:15 EST 2007


On Fri, 2007-01-19 at 00:11 -0700, Ted Bullock wrote:
> Ok, I am still having a bit of trouble here.  The double indirection
> thing is pretty confusing when trying to deal with it across the
> unmanaged/managed barriers.
> 
> [StructLayout(LayoutKind.Sequential)]
> public class DeviceEntry
> {
>   private IntPtr name;
>   private ushort vendor_id;
>   private ushort product_id;
>   private uint device_flags;
> 	
> [DllImport ("libmtp")]
> internal static extern void Get_Supported_Devices(
> 	out IntPtr devicelist,
> 	out int NumDevices);
> 	
> public static DeviceEntry[] SupportedDeviceList()
> {
> IntPtr DeviceList;
> int NumDevices;
> 
> Get_Supported_Devices(out DeviceList, out NumDevices);
> 		
> if (NumDevices <= 0)
>   return new DeviceEntry[]{};
> 
> DeviceEntry[] Devices = new DeviceEntry[NumDevices];
> 		
> for (int i = 0; i < NumDevices; ++i)
> {
>   IntPtr s = Marshal.ReadIntPtr(DeviceList, i *
> 	Marshal.SizeOf(typeof(DeviceEntry)));

My bad.  This *should* be:

	IntPtr s = (IntPtr) (((long) DeviceList) +
		(long) i*Marshal.SizeOf (typeof(DeviceEntry)));

>   Devices[i] = (DeviceEntry)Marshal.PtrToStructure(s,
> 	typeof(DeviceEntry));
> }
> 		
> return Devices;
> }
> 
> }

See also the attached example program.  To run:

	gcc -shared -o libarray.so array.c
	mcs array.cs
	LD_LIBRARY_PATH=`pwd` mono array.exe

 - Jon
       
-------------- next part --------------
A non-text attachment was scrubbed...
Name: array.cs
Type: text/x-csharp
Size: 1105 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20070119/4164d234/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: array.c
Type: text/x-csrc
Size: 497 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20070119/4164d234/attachment-0001.bin 


More information about the Mono-devel-list mailing list