Using FlashMagicARM.dll in C# .Net project

Started by Orfuze, January 09, 2009, 02:30:21 PM

Previous topic - Next topic

Orfuze

Hi, I am trying to use flashMagic.dll in a C# .net project but I've got problem with fm_connect method:

I declare the method like this in a FlashMagic class:


        [StructLayout(LayoutKind.Sequential)]       
        public struct Options
        {
            public int usingicp;       // set to 1 if using icp
            public int interfacetype;  // interface being used (FM_INTERFACETYPE_xxx)
        }

        [DllImport("FlashMagicARM.dll", EntryPoint = "_fm_connect", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Auto, SetLastError=true)]
        private unsafe static extern IntPtr _fm_connect(double osc, int port, long baudrate, int selecteddevice, int highspeed, int clocks, int halfduplex, int hwconfig, int hwt1, int hwt2, byte reserved1, long maxbaudrate, ref IntPtr options);
        public unsafe static Results fm_connect(double osc, int port, long baudrate, int selecteddevice, int highspeed, int clocks, int halfduplex, int hwconfig, int hwt1, int hwt2, byte reserved1, long maxbaudrate, Options options)
        {
            // Initialize unmanged memory to hold the struct.
            IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(options));
            Marshal.StructureToPtr(options, ptr, false);

            Results Res = (Results)Marshal.PtrToStructure(_fm_connect(osc, port, baudrate, selecteddevice, highspeed, clocks, halfduplex, hwconfig, hwt1, hwt2, reserved1, maxbaudrate, ref ptr), typeof(Results));

            Marshal.FreeCoTaskMem(ptr);
            return Res;
        }


And I am calling the method like this in Main method:


            FlashMagic.Options options = new FlashMagic.Options();
            options.usingicp = 0;
            options.interfacetype = (int)FlashMagic.InterfaceType.FM_INTERFACETYPE_NONE;

            FlashMagic.Results r = FlashMagic.fm_connect(12.000, 1, 38400, (int)FlashMagic.Device.FM_LPC2129, 0, 0, 0, (int)FlashMagic.HardwareConfig.FM_HWBOOTEXEC, 200, 200, 0, 115200, options);
            if (r.result != FlashMagic.Result.FM_OK)
            {
                MessageBox.Show(new string(r.details).TrimEnd('\0'));
            }



At the Execution I have got an "AccessViolation exception : Attempted to read or write protected memory. This is often an indication that other memory is corrupt." just when i am calling the method.

Does anybody have an idea ?
Thank you.

Andy Ayre

The calling convention is _cdecl (see the declarations in flashmagic.h). Have you tried that instead of StdCall?

Andy
Embedded Systems Academy, Inc.
support at esacademy dot com