2011-10-21 56 views
0

C++ DLL的函數聲明是傳遞整數數組到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#,我accesssing上述功能作爲

Sort.Sort.BubbleSort(arrayToBeSort,5); 

但C sharp編譯器給出的錯誤爲

關於 'Sort.Sort.BubbleSort(INT *,INT)' 最好的重載的方法匹配具有一些無效參數 和 參數1:不能從 'INT []' 轉換爲 '詮釋*'

+2

如何'Sort.Sort.BubbleSort'在C#代碼中聲明 –

+0

功能是從C# – user1006897

+0

1日訪問Sort是命名空間2nd Sort是一個類名稱 – user1006897

回答

2

陣列在託管的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) 
+0

非常感謝它的工作 – user1006897

+0

如何將此數組作爲C#的參考傳遞給我# – user1006897

+0

對於您不需要的特定問題,可以在不通過引用傳遞數組的情況下修改氣泡排序函數中的數組內容。 –