将整型数组传递给c++ DLL

本文关键字:c++ DLL 整型 数组 | 更新日期: 2023-09-27 18:11:50

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++ DLL

托管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)