简体   繁体   English

如何访问C#函数,它将指针作为C#的输入参数

[英]How to access a C++ function which takes pointers as input argument from a C#

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;
            }
        }
    }
}

It throws an Error Pointers and fixed size buffers may only be used in an unsafe context. 它会抛出错误指针,固定大小的缓冲区只能在不安全的上下文中使用。 How do I pass pointers to the C++ dll? 如何将指针传递给C ++ DLL? I am new to C#, please help me 我是C#的新手,请帮助我

Rather than using an unsafe context try: 而不是使用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);

To make the call: 拨打电话:

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

I'm assuming the SRX_DllVersion parameters are output only, if not change out to ref . 我假设SRX_DllVersion参数仅输出,如果不改变outref

Avoid unsafe code when ever possible. 尽可能避免使用unsafe代码。

Of course you've to mark the class unsafe to make it work. 当然,你要标记这个类是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);
}

If you have only one method you could mark the method as unsafe . 如果您只有一种方法,则可以将该方法标记为unsafe

And don't forget to turn on "allow unsafe code" compiler option as well. 并且不要忘记打开“允许不安全的代码”编译器选项。

You must mark that method as unsafe 您必须将该方法标记为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);

Try to use unsafe block. 尝试使用unsafe阻止。 More about unsafe . 更多关于不安全的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用从C#获取指针的C ++函数 - Calling a C++ Function that takes pointers from C# 如何调用从C ++ dll加载的将指针作为输入的外部函数 - How to call an external function that takes pointers as input, loaded from a C++ dll 有没有办法让 C# 应用程序使用 C++ DLL 提供一个以字符串为参数的回调函数? - Is there a way for a C# application using a C++ DLL to provide a callback function which takes a String as an argument? 如何从C#创建可传递到C ++委托,该委托将IEnumerable作为SWIG的参数? - How to create passable from C# into C++ delegate that takes a IEnumerable as argument with SWIG? 调用从C#获取char指针的C ++函数 - Calling a C++ function that takes a char pointer from C# 使用C#指针迁移函数C ++ - Migrate function C++ with pointers to C# 如何调用C ++函数需要从C#进行Vector调用 - How to call C++ function takes Vector call from C# 如何在C#中使用Reflection调用将输入参数作为另一个类对象的方法? - How to invoke a method which takes input parameter as another class object using Reflection in C#?(Method gives argument exception) C#方法,无论输入/返回类型如何,都将另一种方法作为参数 - C# Method which takes another method as an argument regardless of input/return type 如何使用 C# 中的 ValueType 参数调用 C++/Cli function - How to call C++/Cli function with ValueType argument from C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM