gpt4 book ai didi

c# - 如何在 C# 中从非托管 C++ DLL 正确调用方法?

转载 作者:行者123 更新时间:2023-11-28 05:34:11 25 4
gpt4 key购买 nike

我有一个用 C++ API 控制的采集板。我想直接从我的 C# 应用程序调用这些方法。

这些是导致问题的方法:

动态链接库:

// Read the DATA of the board
SPINAPI int pb_get_data(unsigned int num_points, int *real_data, int *imag_data);
// Write plain ASCII file for the data returned from pb_get_data(..)
SPINAPI int pb_write_ascii(const char *fname, int num_points, float SW, float SF, const int *real_data, const int *imag_data);

其中SPINAPI定义如下:

#ifdef __WINDOWS__
#ifdef DLL_EXPORTS
#define SPINAPI __declspec(dllexport)
#else
#define SPINAPI __declspec(dllimport)
#endif
#else
#define SPINAPI
#endif

C#:

[DllImport(@"C:\SpinCore\SpinAPI\lib32\spinapi.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int pb_get_data(uint num_points, [MarshalAs(UnmanagedType.LPArray)] ref int[] real_data, [MarshalAs(UnmanagedType.LPArray)] ref int[] imag_data);

[DllImport(@"C:\SpinCore\SpinAPI\lib32\spinapi.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int pb_write_ascii(string fname, int num_points, float SW, [MarshalAs(UnmanagedType.LPArray)] ref int[] real_data, [MarshalAs(UnmanagedType.LPArray)] ref int[] imag_data);

调用时 pb_get_data(),

pb_get_data((uint)numberOfPoints, ref idata, ref idata_imag);

我收到以下错误:

System.AccessViolationException was unhandled

Message: An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module.
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

调用时 pb_write_ascii(),

pb_write_ascii(@"data\direct_data_0.txt", numberOfPoints, (float)actualSW, ref idata, ref idata_imag);

哪里:

int[] idata = new int[MAX_NUMBER_POINTS];
int[] idata_imag = new int[MAX_NUMBER_POINTS];

没有任何反应(文件未写入)。

有人知道我做错了什么以及如何纠正这些问题吗?我提到我可以从 DLL 中成功调用其他方法。

最佳答案

根据 Hans Passant 的说法,消除 ref 关键字是造成问题的原因。删除它解决了问题。

关于c# - 如何在 C# 中从非托管 C++ DLL 正确调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38699064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com