【问题标题】:importing a c dll function into c#将c dll函数导入c#
【发布时间】:2010-12-28 04:25:57
【问题描述】:

我正在尝试从未管理的 C dll 中使用 C# 中的函数。我是 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];

我填充了数组,然后尝试将其传递给 Function 并收到错误。我知道 IntPtr 是不对的。这样做的正确方法是什么?

【问题讨论】:

标签: c# dll import


【解决方案1】:

它应该适用于 ushort[]

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

【讨论】:

  • 避免ExactSpelling,C函数通常导出为_Function。 CallingConvention 是必需的,Cdecl。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

    • 不,不要那样做。这将给出一个指针到指针(即ushort **)。
    猜你喜欢
    • 1970-01-01
    • 2016-05-23
    • 2021-01-21
    • 2021-07-04
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多