-1

我在 C++ 中有以下实现(创建了相同的 DLL)

double *getData()
{
    double *eyeTrackData = new double[10];
    const unique_ptr<Fove::IFVRHeadset> headset{ Fove::GetFVRHeadset() };

    CheckError(headset->Initialise(Fove::EFVR_ClientCapabilities::Gaze), 
"Initialise");

    Fove::SFVR_GazeVector leftGaze, rightGaze;
    const Fove::EFVR_ErrorCode error = headset->GetGazeVectors(&leftGaze, 
    &rightGaze);


    // Check for error
    switch (error)
    {

    case Fove::EFVR_ErrorCode::None:
        eyeTrackData[0] = (double)leftGaze.vector.x;
        eyeTrackData[1] = (double)leftGaze.vector.y;
        eyeTrackData[2] = (double)rightGaze.vector.x;
        eyeTrackData[3] = (double)rightGaze.vector.y;
        break;


    default:
        // Less common errors are simply logged with their numeric value
        cerr << "Error #" << EnumToUnderlyingValue(error) << endl;
        break;
    }

    return eyeTrackData;
}

我已经包括

extern "C"
{
    __declspec(dllexport) double *getData();
}

在头文件中。

我尝试用 C-sharp 接收这个。

[DllImport("C:\\Users\\BME 320 - Section 1\\Documents\\Visual Studio 2015\\Projects\\EyeTrackDll\\x64\\Debug\\EyeTrackDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr eyeData();

但我不知道如何在 buttonClick 事件中接收数组。

我很感激这方面的任何帮助。

4

1 回答 1

0

按照@DavidHeffernan 的提示,我将尝试为您提供一个如何实现目标的示例。请把 C# 部分当作伪代码,因为我不是那种语言的专家。

我会使用前面所说的结构,只是为了让事情更清楚,我认为提供一些类型保护以及在不知道确切有多少双精度的情况下向结构添加“大小”属性的可能性将是一个好习惯是。在你的情况下,像往常一样是 4,是不需要的。

C++ 库中的函数:

void getData(double* eyeTrackData)
{
    const unique_ptr<Fove::IFVRHeadset> headset{ Fove::GetFVRHeadset() };

    CheckError(headset->Initialise(Fove::EFVR_ClientCapabilities::Gaze), "Initialise");

    Fove::SFVR_GazeVector leftGaze, rightGaze;
    const Fove::EFVR_ErrorCode error = headset->GetGazeVectors(&leftGaze, &rightGaze);


    // Check for error
    switch (error)
    {

    case Fove::EFVR_ErrorCode::None:
        eyeTrackData[0] = (double)leftGaze.vector.x;
        eyeTrackData[1] = (double)leftGaze.vector.y;
        eyeTrackData[2] = (double)rightGaze.vector.x;
        eyeTrackData[3] = (double)rightGaze.vector.y;
        break;

    default:
        // Less common errors are simply logged with their numeric value
        cerr << "Error #" << EnumToUnderlyingValue(error) << endl;
        break;
    }
}

C# 方面:

[DllImport("C:\\Users\\BME 320 - Section 1\\Documents\\Visual Studio 2015\\Projects\\EyeTrackDll\\x64\\Debug\\EyeTrackDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void eyeData(IntPtr); 

private void button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
        IntPtr ipr = Marshal.AllocHGlobal(4); // Memory blob to pass to the DLL
        eyeData(ipr);
        double[] eyeTrackData = new double[4]; // Your C# data
        Marshal.Copy(ipr, eyeTrackData, 0, 4); // Convert?
    }
    finally 
    { 
        Marshal.FreeHGlobal(ipr); 
    }
} 

再次为我的“糟糕的 C#”xD 感到抱歉。希望这可以帮助。

于 2018-06-13T20:18:20.267 回答