[Mono-bugs] [Bug 495757] New: Marshaling seems to fail with structures as return value from unmanaged c-progs

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Thu Apr 16 19:13:59 EDT 2009


http://bugzilla.novell.com/show_bug.cgi?id=495757


           Summary: Marshaling seems to fail with structures as return
                    value from unmanaged c-progs
    Classification: Mono
           Product: Mono: Runtime
           Version: 2.0.x
          Platform: i386
        OS/Version: openSUSE 11.1
            Status: NEW
          Severity: Major
          Priority: P5 - None
         Component: interop
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: heertsch at endotherm.ch
         QAContact: mono-bugs at lists.ximian.com
          Found By: Component Test


the following libusb test fails: Marshalling returns arbitrary data (mostly 0).
The same test runs in VS2005 and libusb-win32 fine. I'm afraid, this could be a
bug (if I did no mistake)... Insert the following code as main in a console
application in monodevelop 2.0

/*include     
<dllmap dll="libusb0.dll" target="libusb.so" os="!windows" />
in /etc/mono/config */

using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
namespace libusbtest
{
    [SuppressUnmanagedCodeSecurity]
    class MainClass
    {
        private const string LIBUSB_DLL = "libusb0.dll";
        private const CallingConvention CC = CallingConvention.Cdecl;
        #region DLLIMPORT EXTERN METHODS
        internal struct libusb
        {
            internal IntPtr prev;
            internal IntPtr next;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 512)]
             internal string sDir;
            internal IntPtr Dev;
            internal int iLoc;
            internal IntPtr root;
        }
        [DllImport(LIBUSB_DLL, CallingConvention = CC, SetLastError = true)]
        public static extern int usb_find_busses();
        [DllImport(LIBUSB_DLL, CallingConvention = CC, SetLastError = true)]
        public static extern int usb_find_devices();
        [DllImport(LIBUSB_DLL, CallingConvention = CC, SetLastError = true)]
        public static extern IntPtr usb_get_busses();
        [DllImport(LIBUSB_DLL, CallingConvention = CC, SetLastError = true)]
        public static extern void usb_init();
        [DllImport(LIBUSB_DLL, CallingConvention = CC)]
        public static extern string usb_strerror();
        #endregion
        public static void Main(string[] args)
        {   
            usb_init();
            int iBus=usb_find_busses();
            int iDev=usb_find_devices();
            Console.WriteLine("Busses: {0} Devices:
{1}",iBus.ToString(),iDev.ToString());
            IntPtr pBusses=usb_get_busses();
            libusb bus=(libusb)Marshal.PtrToStructure(pBusses,typeof(libusb));
            if (bus.root!=IntPtr.Zero)Console.WriteLine("Bingo!! It works!");
            else Console.WriteLine("Pointer to Device is null! ;(");
            Console.WriteLine("\nList of Bytes in  bus-struct >0");
            for(int i=0;i<Marshal.SizeOf(bus);i++)
            {
                byte bB=Marshal.ReadByte(pBusses,i);
                if(bB>0)Console.WriteLine("offset: {0} value:
{1}",i.ToString(),bB.ToString());
            }
        }
    }
}

-- 
Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list