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