title: DLLImport when c++ function involves a callback containing a struct
body: <p>We have a c++ function </p>
  
  <p><code>IMPEXP int startGettingData(LPCWSTR port, ClubConfig* pClubConfig, void (__stdcall *onSwingCalculated)(CalculationResult* pResult));</code></p>
  
  <p>The Calculation result is a struct in c++</p>
  
  <pre><code>typedef struct stCalculationResult
  {
  BOOL    isLeftHanded;   
  double  lie;            
  double  open;           
  double  loft;           
  double  attack;         
  double  startLie;       
  double  startOpen;      
  double  startLoft;      
  double  faceToPath;     
  double  clubPath;       
  double  startLean;      
  double  endLean;        
  double  startTime;      
  double  stopTime;       
  double  tempo;          
  double  clubHeadSpeed;
  int32_t numberOfPositions;  
  int32_t vectorLength;
  int32_t matrixLength;
  int32_t impact;             
  int32_t tobs;               
  int32_t halfway;            
  int32_t quarterway;         
  int32_t three_quarterway;   
  int32_t rawDataLength;      
  double* CorrPositions;     
  double* Orientations;       
  double* HVelocities;        
  double* HAngularVelocities;
  int32_t* rawData;
  } CalculationResult;
  </code></pre>
  
  <p>I am trying to figure out how to call this function in c#  This is what I have so far</p>
  
  <pre><code>[DllImport("my.dll")]
  static extern Int32 startGettingData([MarshalAs(UnmanagedType.LPWStr)] string port, [MarshalAs(UnmanagedType.LPStruct)] ClubConfig pClubConfig, onSwingCalculate d);
  </code></pre>
  
  <p>and the delegate</p>
  
  <pre><code>public delegate void onSwingCalculate(IntPtr data);
  </code></pre>
  
  <p>and the struct</p>
  
  <pre><code>public struct CalculationResult
  {
      public bool isLeftHanded;       
      public double lie;             
      public double open;         
      public double loft;         
      public double attack;           
      public double startLie; 
      public double startOpen;
      public double startLoft;
      public double faceToPath;
      public double clubPath; 
      public double startLean;
      public double endLean;  
      public double startTime;
      public double stopTime; 
      public double tempo;    
      public double clubHeadSpeed;
  
      public Int32 numberOfPositions;
      public Int32 vectorLength;
      public Int32 matrixLength;
      public Int32 impact;                
      public Int32 tobs;              
      public Int32 halfway;           
      public Int32 quarterway;            
      public Int32 three_quarterway;  
      public Int32 rawDataLength;      
      public IntPtr CorrPositions;     
      public IntPtr Orientations;      
      public IntPtr HVelocities;        
      public IntPtr HAngularVelocities;
      public IntPtr rawData;            
  }
  </code></pre>
  
  <p>and finally the actual callback function</p>
  
  <pre><code>void SwingCalculate(IntPtr data)
  {
      CalculationResult result = new CalculationResult();
      result = (CalculationResult)Marshal.PtrToStructure(data, typeof(CalculationResult));
      Debug.Log("Clubhead speed: " +result.clubHeadSpeed);
  }
  </code></pre>
  
  <p>Unfortunatly when the callback function receives data it disconnects.</p>