我如何在C#中调用/整理C ++“ const uint64_t *”?

问题描述 投票:1回答:1

我过去使用过许多非托管C ++ DLL,但遇到了我从未见过的类型。我首次尝试使用它来结束爆炸。 :)

这里是C ++函数签名:

 DLL_EXPORTS int MUSH_ProcessBuffer(uint64_t NumEntries, const uint64_t* AbsTimeNs, const uint64_t* Events);

AbsTimeNs和Events均是在我的C#代码中传递的无符号长数组。 NumEntries是各个数组的长度。

我已经尝试了以下两项:

[DllImport("mush.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int MUSH_ProcessBuffer(UInt64 NumEntries, ref ulong[] AbsTimeNS, ref ulong[] Events);

[DllImport("mush.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int MUSH_ProcessBuffer(UInt64 NumEntries, ref UInt64[] AbsTimeNS, ref UInt64[] Events);

都不起作用...我得到一个例外:{“试图读取或写入受保护的内存。这通常表明其他内存已损坏。”}我不确定类型是否错误或是否必须用函数签名中的“ const”或做什么。万一这是我要传递的信息,而不是pinvoke本身,这是C#代码:

ulong[] timeArray = absTimes.ToArray();
ulong[] eventArray = events.ToArray();

NativeMethods.MUSH_ProcessBuffer((ulong)absTimes.Count, ref timeArray, ref eventArray);
c# pinvoke marshalling unmanaged
1个回答
2
投票

您需要在方法定义中删除ref关键字。

一个数组已经被引用传递,添加ref为您提供了另一个指向该引用的指针。

在C中,它将是const uint64_t**而不是const uint64_t*

所以:

[DllImport("mush.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int MUSH_ProcessBuffer(ulong NumEntries, ulong[] AbsTimeNS, ulong[] Events);
© www.soinside.com 2019 - 2024. All rights reserved.