我在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事件中接收数组。
我很感激你的帮助。
答案 0 :(得分:0)
根据@DavidHeffernan提示,我将尝试给您一个如何实现目标的示例。请把C#部分作为伪代码,因为我不是那种语言的专家。
如前所述,我会使用一个结构,只是为了让事情变得更清楚,我认为给一些类型保护是一个好习惯,并且如果不知道如何确定如何为结构添加“size”属性的可能性很多双打都会。在你的情况下,一如既往是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。希望这会有所帮助。