[Mono-dev] libmtp bindings

Ted Bullock tbullock at canada.com
Thu Jan 18 05:15:34 EST 2007

I am attempting to write mono bindings for the libmtp library as a part
of a final year software engineering project.  Although I have worked on
a couple of smaller mono projects while at school, I have never directly
worked with the managed/unmanaged aspects of the runtime.

Right now I am puzzled by something that I feel should be rather
straightforward although I have been trying to find for the better part
of a day now. I have read through the MSDN unmanaged interop section,
the interop parts of the monodoc wiki and the following website
(http://mono-project.com/Interop_with_Native_Libraries) with no luck.
Hopefully somebody with more experience than me here on the list can help

Here is the problem; I am trying to write a binding for the following c
function call:

void Get_Devices_List(device_entry_t ** const, int * const);

Notice the location of const.  The target is constant, but the pointer

[DllImport ("libmtp")]
internal static extern void LIBMTP_Get_Supported_Devices_List(
	out IntPtr devicelist,
	out int NumDevices);

The function I am trying to write is as follows which simply returns an
array of the devices pointed to by device_entry_t:  Note that NumDevices
is correctly filled out with a value 44.

public static DeviceEntry[] SupportedDeviceList()
IntPtr DeviceList = IntPtr(0);
int NumDevices = 0;

Get_Devices_List(out DeviceList, out NumDevices);
if (NumDevices <= 0)
  return new DeviceEntry[]{};
DeviceEntry[] Devices = new DeviceEntry[NumDevices];
for (int i = 0; i < NumDevices; ++i)
IntPtr p = Marshal.ReadIntPtr(DeviceList, i * IntPtr.Size);
//*************** Crash on next line ********************
Devices[i] =(DeviceEntry)Marshal.PtrToStructure(p,typeof(DeviceEntry));

return Devices;

Hope somebody can help!

-Ted Bullock

More information about the Mono-devel-list mailing list