locked
PInvoke marshal array problem? RRS feed

  • Question

  • Hi i've got the following method from a dll written in c:

    extern "C" short CCONV acq_get_data ( 
     short board_id, short source, BoGrams_status *status, 
     long npts, 
     float *data,  //problem 
     long *interf_npts, //problem
     float *interf_data //problem
    );
    

    Now my problem the last 3 parameters are pointers to arrays but at least the float arrays are not blittable in C# so how do i fix this?

    In C# I call the method as following:

    [System.Runtime.InteropServices.DllImportAttribute("BOGRAMS.dll", EntryPoint = "acq_get_data", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)] 
    public static extern short acq_get_data( 
     short board_id, short source, BoGrams_status status, long npts, 
     float[] data, 
     long[] interf_npts, 
     float[] interf_data
    ); 
    
    

    But then the following exeption occurs:

    AccesViolationExeption was unhandled

    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Thanks in advance for any help!

    Monday, August 16, 2010 1:20 PM

Answers

  • You didn't provide enough information for us to answer.  Based on the fact you need to add "ref" then your definition of BoGrams_status is a struct.  If it had been a class then the 'ref' would not be necessary.
    • Marked as answer by warrentang Monday, August 23, 2010 10:58 AM
    Wednesday, August 18, 2010 1:00 PM

All replies

  • I don't think arrays are the issue. I would guess the problem is because you declared 'npts' parameter as long in C# side while it should be int.

    You see, long is 64 bit in C# and 32 bit in C++ so they don't match messing up everything after this parameter. There are other cases like this, e.g. BOOL and bool.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, August 16, 2010 3:56 PM
  • Are the arrays "In" or "Out" parameters?

    Would something like this do the trick?:
    [DllImportAttribute("BOGRAMS.dll", EntryPoint = "acq_get_data", CallingConvention = CallingConvention.StdCall)] 
    public static extern short acq_get_data( 
     short board_id, short source, BoGrams_status status, long npts, 
     [MarshalAs(UnmanagedType.LPArray)]float[] data, 
     [MarshalAs(UnmanagedType.LPArray)]long[] interf_npts, 
     [MarshalAs(UnmanagedType.LPArray)]float[] interf_data
    ); 
    
    

    Monday, August 16, 2010 4:01 PM
  • long or int makes no difference it both gives the same error. It must be the arrays the dll has a lot more methods with normal/object parameters and the work fine. This is the only method who uses array pointers and the only method who fails:(
    Monday, August 16, 2010 5:35 PM
  • Output parameters

    The dll comunnicates with a measuring instrument the array parameters contain information and data about the measurment the other parameters are input parameters.

    Monday, August 16, 2010 5:44 PM
  • Are you passing arrays from managed code? Are these of proper size? Did you fix another 'long' mismatch in the array type? Can you attach another instance of VS, this time native device project, put a break point in native function to see what exactly are you getting from managed code?


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, August 16, 2010 5:54 PM
  • Found the solution:

        [System.Runtime.InteropServices.DllImportAttribute("BOGRAMS.dll", EntryPoint = "acq_get_data", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
        public static extern short acq_get_data(
          short board_id, short source,
          ref BoGrams_status status,
          int npts,
          float[] data,
          int[] interf_npts,
          float[] interf_data
          );
    

    This works the problem was the status object whith needed the keyword ref.

    thx for the all the effort and help

    Wednesday, August 18, 2010 7:42 AM
  • You didn't provide enough information for us to answer.  Based on the fact you need to add "ref" then your definition of BoGrams_status is a struct.  If it had been a class then the 'ref' would not be necessary.
    • Marked as answer by warrentang Monday, August 23, 2010 10:58 AM
    Wednesday, August 18, 2010 1:00 PM