【问题标题】:Passing integer array to a c++ DLL将整数数组传递给 C++ DLL
【发布时间】:2011-10-21 10:35:46
【问题描述】:

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 语言编译器会报错

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

【问题讨论】:

  • C#代码中Sort.Sort.BubbleSort是如何声明的
  • 第一次排序是命名空间第二次排序是一个类名
  • 如果你需要帮助你需要显示BubbleSort的C#声明。
  • 冒泡排序的 C# 声明private void BtnSubmitClick(object sender, EventArgs e) { Sort.Sort.BubbleSort(arrayToBeSort,5); for (int i = 0; i
  • 不,这就是你所说的。您还没有出示您的声明。

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


【解决方案1】:

托管 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# 的引用传递
  • 对于这个你不需要的特殊问题,你可以在冒泡排序函数中修改数组的内容,而不需要通过引用传递数组。
猜你喜欢
  • 1970-01-01
  • 2013-02-26
  • 2021-01-29
  • 1970-01-01
  • 2010-12-15
  • 2020-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多