c# - 将整数数组传递给 C++ DLL

标签 c# c++ visual-c++

C++ dll函数声明为

static void __clrcall BubbleSort(int* arrayToSort,int size);

我的C++ dll函数是

void Sort::BubbleSort(int* sortarray,int size)
    {
        int i,j;
        int temp=0;
       for(i=0; i< (size - 1); ++i)
        {
            for(j = i + 1; j > 0; --j)
            {
                if(sortarray[j] < sortarray[j-1])
                {
                    temp = sortarray[j];
                    sortarray[j] = sortarray[j - 1];
                    sortarray[j - 1] = temp;
                }
            }
        }
    }

在 C# 中,我正在访问上面的函数作为

Sort.Sort.BubbleSort(arrayToBeSort,5);

但是 C sharp 编译器给出的错误是

“Sort.Sort.BubbleSort(int*, int)”的最佳重载方法匹配有一些无效参数 和 参数 1:无法从“int[]”转换为“int*”

最佳答案

托管 C++ 中的数组需要使用托管语法。

static void __clrcall BubbleSort(array<int>^ arrayToSort, int size)

这在 C# 中转换为

public static void BubbleSort(int[] array, int size);

您的声明改为使用指针(不安全代码)匹配 C# 声明。

public static void BubbleSort(int* array, int size);

如果你需要通过引用传递一个值,你应该这样写:

static void __clrcall MyFunc(array<int>^% arrayByReference)

关于c# - 将整数数组传递给 C++ DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7848225/

相关文章:

c++ - 优化器错误或未定义的行为?

c# - 转换 IQueryable<object> 时出现 NotSupportedException

c# - 更新到VS 16.9.0 : Feature 'init-only setters' is not available in C# 7. 3之后。请使用语言版本9.0或更高版本

c# - 使用 Visual Studio 和 NUnit 连接到 localdb

c++ - 嵌入式C++——多态与继承

c++ - c99 : 63 characters of an internal name are significant?

c++ - 如何验证 Visual Studio 2017 中使用的 Spectre Mitigation 标志是否适用于 C++?

c# - 将 XML 转换为格式化文本小瓶 XSLT

c++ - 为什么 VC++ 无法优化整数包装器?

c++ - std::numeric_limits<double>::min() 上的错误 C2589