user1006897
user1006897

Reputation: 13

Passing integer array to a c++ DLL

C++ dll function declaration is

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

My C++ dll function is

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

In C#, I am accesssing above function as

Sort.Sort.BubbleSort(arrayToBeSort,5);

But C sharp compiler gives error as

The best overloaded method match for 'Sort.Sort.BubbleSort(int*, int)' has some invalid arguments and Argument 1: cannot convert from 'int[]' to 'int*'

Upvotes: 0

Views: 2523

Answers (1)

Salvatore Previti
Salvatore Previti

Reputation: 9050

Arrays in managed C++ need to use the managed syntax.

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

This translates in C# to

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

Your declaration, instead matches the C# declaration using pointers (unsafe code).

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

If you need to pass a value by reference you should write something like this:

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

Upvotes: 2

Related Questions