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

时间:2016-08-01 12:34:42

标签: c# c++ dll unmanaged

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

这些是导致问题的方法:

DLL:

// 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中的其他方法。

2 个答案:

答案 0 :(得分:1)

根据Hans Passant的说法,删除ref关键字导致了问题。删除它解决了这个问题。

答案 1 :(得分:0)

使用C ++ / Cli包装库是可能的解决方案。 您应该创建C ++ / Cli库,其中包含以下代码:

namespace ManagedCode {

    public ref class WrappedFunctions
    {
    public: 
        static int pb_get_data(unsigned int num_points, array<int>^ real_data, array<int>^ imag_data)
        {
            pin_ptr<int> temp_real_data = &real_data[0];
            pin_ptr<int> temp_imag_data = &imag_data[0];
            return UnManagedCode::pb_get_data(num_points, temp_real_data, temp_imag_data);
        }
    };
}

在其他情况下,您可以使用编组功能

有关详细信息,请阅读 https://msdn.microsoft.com/ru-ru/library/1dz8byfh.aspx