1

我们有一个 c++ 函数

IMPEXP int startGettingData(LPCWSTR port, ClubConfig* pClubConfig, void (__stdcall *onSwingCalculated)(CalculationResult* pResult));

计算结果是 c++ 中的结构

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;

我想弄清楚如何在 c# 中调用这个函数这是我到目前为止所拥有的

[DllImport("my.dll")]
static extern Int32 startGettingData([MarshalAs(UnmanagedType.LPWStr)] string port, [MarshalAs(UnmanagedType.LPStruct)] ClubConfig pClubConfig, onSwingCalculate d);

和代表

public delegate void onSwingCalculate(IntPtr data);

和结构

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;            
}

最后是实际的回调函数

void SwingCalculate(IntPtr data)
{
    CalculationResult result = new CalculationResult();
    result = (CalculationResult)Marshal.PtrToStructure(data, typeof(CalculationResult));
    Debug.Log("Clubhead speed: " +result.clubHeadSpeed);
}

不幸的是,当回调函数接收到数据时,它会断开连接。

4

0 回答 0