[Mono-list] How to get linux constants like PATH_MAX into mono

Jonathan Pryor jonpryor at vt.edu
Tue Apr 21 14:58:22 EDT 2009


On Tue, 2009-04-21 at 18:08 +0200, Andreas Heertsch wrote:
> I try to port libusb via LibUsbDotNet to Linux/OSX. The authors from
> libusb wrote structures with PATH_MAX as field lenght. So I have to
> integrate this malformed code. :(

You're screwed.

The problem is that the array is inline:

        struct usb_device {
          struct usb_device *next, *prev;
        
          char filename[PATH_MAX + 1];
          /* ... */
        };

There are really only two ways to easily deal with this in C# [0], by
using [MarshalAs]:

        struct UsbDevice {
          [MarshalAs(UnmanagedType.ByValArray, SizeConst=???)]
          public byte[] filename;
          // ...
        }

or by using fixed arrays:

        unsafe struct UsbDevice {
          public fixed byte filename[???];
        }

Both of these require compile-time constant values, which
Syscall.pathconf() cannot provide.

I think in this case the only viable solution is a MonoPosixHelper-style
wrapper which exposes C#-friendly structures and functions, e.g.

        /* C */
        struct ManagedUsbDevice {
          void* next;
          void* prev;
          char* filename;
          void* bus;
          /* ... */
        };
        
        int UsbHelper_ToManagedUsbDevice(struct usb_device *from, struct ManagedUsbDevice *to);
        int UsbHelper_FromManagedUsbDevice(struct ManagedUsbDevice *from, struct usb_device *to);

Alternatively, forget structure compatibility from C# and just expose
IntPtr to the C# code, and write a set of accessor functions which
return struct fields.

 - Jon

[0]
http://www.mono-project.com/Dllimport#Arrays_Embedded_Within_Structures


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20090421/20ab804c/attachment.html 


More information about the Mono-list mailing list