Swinging Moose
Swinging Moose

Reputation: 83

Calling unmanaged C++ library (dll) from C# : How to properly pass data between managed and unmanaged code

I have been working with an interface which read measurement data from a sensor and use a library written in C++ to analyse that data.

The function is just about the following:

  1. Set measurement parameters on the C++ library
  2. Get data from the sensor (1200 measurements alltogether)
  3. Write data to C++ library
  4. Process all 1200 measurements in C++ library
  5. Read results from C++ library

Calling this C++ library from C#-code is previously dealt with this question: Calling unmanaged C++ library (dll) from C# creates an access violation error (0xc0000005).

Now it seems, that the C++ library either

Bad thing is, that I'm not able to debug this C++ library.

What is wrong with my code?

1) Setting the measurement parameters

namespace PdWaveApi
{
[StructLayout(LayoutKind.Sequential)]
public struct PDDataInfo

{                   
   public int   nPings;              
   public int   nDataRate;          
   public int   nSamples;            
   public float fFrequency;          
   public float fBeamAngle;          
   public int   nInstrument;         
   public int   nCoordSystem;        
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
   public short[] hBeamToXYZ;       
   public short hWaveT1;

    // Constructor
   public static PDDataInfo Create()
   {
       PDDataInfo DataStruct = new PDDataInfo();
       DataStruct.hBeamToXYZ = new short[9];
       return DataStruct;
   }
   }
}

public class PdWaveBaseLWrapper
{
   [DllImport("PdWaveBase.dll", EntryPoint = "PDSetInstrumentConfig")]
   public static extern int PDSetInstrumentConfig(ref PDDataInfo pDataInfo);
}

public void SetInstrumentConfiguration()
{
    PdWaveApi.PDDataInfo InstrumentConfiguration = new PdWaveApi.PDDataInfo();
    .................
    Initializing the InstrumentConfiguration structure
    ...............
    PdWaveBaseLWrapper.PDSetInstrumentConfig(ref InstrumentConfiguration);
}

3) Reading data from sensor and writing data to C++ library

namespace PdWaveApi
{
    [StructLayout(LayoutKind.Sequential)]
    public struct PDWaveSample
    {   
        [MarshalAs(UnmanagedType.I1)]
        public bool Valid;
        public float fPressure;
        public float fDistance;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.PD_MAX_WAVEBEAMS)]
        public float[] fVel;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.PD_MAX_WAVEBEAMS)]
        public ushort[] nAmp;

        // Constructor

        public static PDWaveSample Create()
        {
            PDWaveSample DataStruct = new PDWaveSample();
            DataStruct.fVel = new float[Constants.PD_MAX_WAVEBEAMS];
            DataStruct.nAmp = new ushort[Constants.PD_MAX_WAVEBEAMS];
            return DataStruct;
        }
    }
}

public class PdWaveBaseLWrapper
{
    [DllImport("PdWaveBase.dll", EntryPoint = "PDSetWaveSample")]
    public static extern int PDSetWaveSample(ref PDWaveSample pWaveSample);
}

namespace SensorInterface
{
    public partial class frmSensorInterface : Form
    {
        public PdWaveApi.PDWaveSample WaveSampleData = PdWaveApi.PDWaveSample.Create();

        private void OnNewData(object sender, OnNewDataEvent e)
        {
            ReadWaveSample(ref WaveSampleData);
            SetWaveSample(ref WaveSampleData);
        }

        public void ReadWaveSample(ref PdWaveApi.PDWaveSample WaveSampleData)
        {
            DateTime MeasurementTimeStamp;
            float[] dVel = new float[4];
            float dTemperature = new float();
            float dPitch = new float();
            float dRoll = new float();
            float dHeading = new float();
            float dPressure = new float();
            short[] sAmp = new short[4];

            //  Read some of the latest data from the control
            GetVelocity(ref dVel[0], ref dVel[1], ref dVel[2], ref dVel[3]);
            GetAmplitude(ref sAmp[0], ref sAmp[1], ref sAmp[2], ref sAmp[2]);

            ..............
            // Set other data to the structure

        }

        public void SetWaveSample(ref PdWaveApi.PDWaveSample WaveSampleData)
        {
            PdWaveBaseLWrapper.PDSetWaveSample(ref WaveSampleData);
        }
    }
}

4) Process all 1200 measurements in C++ library

[StructLayout(LayoutKind.Sequential)]
public struct PDWaveBurst
{
    [MarshalAs(UnmanagedType.ByValArray , SizeConst = Constants.PD_MAX_WAVEMEAS_AST)]    
    public float[] fST; 
    public float fWinFloor;
    public float fWinCeil;
    [MarshalAs(UnmanagedType.I1)]
    public bool bUseWindow;
    [MarshalAs(UnmanagedType.I1)]
    public bool bSTOk;
    [MarshalAs(UnmanagedType.I1)]
    public bool bGetRawAST;
    [MarshalAs(UnmanagedType.I1)]
    public bool bValidBurst;

    public static PDWaveBurst Create()
    {
        PDWaveBurst DataStruct = new PDWaveBurst();
        DataStruct.fST = new float[Constants.PD_MAX_WAVEMEAS_AST];
        return DataStruct;
    }
}

[DllImport("PdWaveBase.dll", EntryPoint = "PDPreProcess")]
public static extern int PDPreProcess(int nSample, ref PDWaveBurst pWaveBurst);

[DllImport("PdWaveBase.dll", EntryPoint = "PDProcessReturnInt")]
public static extern int PDProcessReturnInt();

public void PreprocessBurstData(int nSamples)
{
    PdWaveApi.PDWaveBurst WaveBurstData = PdWaveApi.PDWaveBurst.Create();

    WaveBurstData.fST = new float[4096];
    WaveBurstData.fWinFloor = (float)1.25;
    WaveBurstData.fWinCeil = 2;
    WaveBurstData.bUseWindow = false;
    WaveBurstData.bSTOk = false;
    WaveBurstData.bGetRawAST = false;
    WaveBurstData.bValidBurst = false;

    PdWaveBaseLWrapper.PDPreProcess(nSamples, ref WaveBurstData);
}

public void ProcessData()
{
    int ProcessError = PdWaveBaseLWrapper.PDProcessReturnInt();
}

5) Read results from C++ library

[StructLayout(LayoutKind.Sequential)]
public struct PDWavePar {
   public float fTm02;  
   public float fTp;     
   public float fDirTp;  
   public float fSprTp;  
   public float fMainDir;
   public float fUI;     
   public float fHm0;   
   public float fH3;
   public float fT3;    
   public float fH10; 
   public float fT10;
   public float fHmax;
   public float fTmax;
   public float fTz;
   public float fMeanPres;
   public int   nNumNoDet;
   public int   nNumBadDet;
   public int   nErrCode;  
   public int   nSpectrum; 
   public float fMeanAST;
}


[DllImport("PdWaveBase.dll", EntryPoint = "PDGetWavePar")]
public static extern int PDGetWavePar(ref PDWavePar pwWavePar);

public void GetOutput()
{
    PdWaveApi.PDWavePar WaveParameters = new PdWaveApi.PDWavePar();
    PdWaveBaseLWrapper.PDGetWavePar(ref WaveParameters);
}

So, as conclusion:

What should I change in my code - to pass data correctly to unmanaged dll - to have dll hold and process the data in its' internal structures - to read results correctly from unmanaged code to my C# program?

(Sorry about the length of my question.)

Upvotes: 2

Views: 4178

Answers (1)

Swinging Moose
Swinging Moose

Reputation: 83

The final solution to my problem was to change the alignment to 1 byte, like this:

C# struct definitions: From:

    [StructLayout(LayoutKind.Sequential)]

to

    [StructLayout(LayoutKind.Sequential, Pack=1)]

This has been discussed at least here: Calling C++ metro dll from c# metro app and returning filled structures

Upvotes: 1

Related Questions