将c dll函数导入c#

将c dll函数导入c#,c#,dll,import,C#,Dll,Import,我试图在C#中使用来自未管理的C dll的函数。我是C#的新手,不确定我做得是否正确。C中的函数如下所示: unsigned short Function(unsigned short, unsigned long, unsigned long, unsigned short*); [DllImport("cDLLfile.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private sta

我试图在C#中使用来自未管理的C dll的函数。我是C#的新手,不确定我做得是否正确。C中的函数如下所示:

unsigned short Function(unsigned short, unsigned long, unsigned long, unsigned short*);


[DllImport("cDLLfile.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern short Function(ushort a, UInt32 b, UInt32 c, IntPtr buffer);
缓冲区是一个数组,类似于

ushort[] = new ushort[7];
我填充数组,然后尝试将其传递给函数,结果得到一个错误。我知道IntPtr是不对的。正确的方法是什么?

试试以下方法:

extern short Function(ushort a, UInt32 b, UInt32 c, ushort[] buffer)

它应该与ushort[]配合使用

[DllImport("cDLLfile.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true) ]
private static extern short Function(ushort a, UInt32 b, UInt32 c, ushort[] buffer);

您将找到有关如何封送数组的详细信息和示例代码。

您能发布您收到的错误吗?似乎是避免精确拼写的重复,C函数通常作为_函数导出。呼叫约定是必需的,Cdecl。不,不要尝试。这将给出指向指针的指针(即
ushort**
)。