【问题标题】:How to access a C++ function which takes pointers as input argument from a C#如何访问将指针作为 C# 的输入参数的 C++ 函数
【发布时间】:2013-10-27 18:06:13
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PatternSequencer
{
    class Version
    {
        public string majorVersion;
        public string minorVersion;

        ushort* pmajorVersion;
        ushort* pminorVersion;
        ulong status;

        [DllImport(@"c:\DOcuments\Myapp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")]
        public static extern ulong SRX_DllVersion(ushort* pmajorVersion, ushort* pminorVersion);


        public Version()
        {
            status = SRX_DllVersion(&pmajorVersion, &pminorVersion);
            if (status)
            {
                majorVersion = "1 - " + *pmajorVersion;
                minorVersion = "1 - " + *pminorVersion;
            }
            else
            {
                majorVersion = "0 - " + *pmajorVersion;
                minorVersion = "0 - " + *pminorVersion;
            }
        }
    }
}

它会抛出一个错误指针,并且固定大小的缓冲区只能在不安全的上下文中使用。如何将指针传递给 C++ dll?我是 C# 新手,请帮助我

【问题讨论】:

  • 将参数声明为ref,这样就不需要unsafe代码了。
  • @William:你能详细解释一下吗?我是 C# 新手
  • 您想在此处执行的操作称为Marshaling。这是一个很大的话题,但是对于像这样的简单案例来说,它并不太难。
  • 所以我说 [DllImport(@"c:\FreeStyleBuild\BERTScope\Release\Bin\BitAlyzerDLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")] public static extern ulong SRX_DllVersion (参考 ushort* pmajorVersion,参考 ushort* pminorVersion);但它仍然有一些错误。
  • @user2495173 我把这个例子放在一个答案中,它应该适合你。

标签: c# c++ pointers


【解决方案1】:

您必须将该方法标记为unsafe

[DllImport(@"c:\FreeStyleBuild\BERTScope\Release\Bin\BitAlyzerDLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")]
public static extern ulong SRX_DllVersion(out ushort pmajorVersion, out ushort pminorVersion);

【讨论】:

  • @user2495173 如果您觉得相关,请编辑您的问题以包含完整代码。
  • 该语法可用于作为方法(或构造函数、访问器)主体的一部分的块。整个类成员不在块内。
  • @JeppeStigNielsen 你完全正确。我已经更新了我的答案。
【解决方案2】:

当然,您必须标记课程 unsafe 以使其正常工作。

unsafe class Version
{
    [DllImport(@"c:\FreeStyleBuild\BERTScope\Release\Bin\BitAlyzerDLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")]
     public static extern ulong SRX_DllVersion(ushort* pmajorVersion, ushort* pminorVersion);
}

如果您只有一种方法,您可以将该方法标记为unsafe

别忘了打开“允许不安全代码”编译器选项。

【讨论】:

    【解决方案3】:

    而不是使用unsafe 上下文尝试:

    [DllImport(@"c:\FreeStyleBuild\BERTScope\Release\Bin\BitAlyzerDLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SRX_DllVersion")]
    public static extern ulong SRX_DllVersion(out ushort pmajorVersion, out ushort pminorVersion);
    

    拨打电话:

    ushort major, minor;
    SRX_DllVersion(out major, out minor);
    

    我假设 SRX_DllVersion 参数仅输出,如果不将 out 更改为 ref

    尽可能避免使用unsafe 代码。

    【讨论】:

    • 这绝对是要走的路;完全没有理由在这里使用不安全的代码。
    • 完美运行!!非常感谢 :) 我在哪里可以了解更多信息?
    • Marshaling Data with Platform InvokePInvoke.net 如果您需要样品或普通进口。
    【解决方案4】:

    尝试使用unsafe 块。 更多关于unsafe

    【讨论】:

      猜你喜欢
      • 2011-07-11
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多