首先c++的**為:
extern "c" __declspec(dllexport) int add(int x, int y);
extern "c" __declspec(dllexport) int onearrayoutmemory(int* arrays, int length);
extern "c" __declspec(dllexport) int onearrayinmemory(int* &arrays, int &length);
extern "c" __declspec(dllexport) int onearrayinmemoryfree(int*& arrays);
extern "c" __declspec(dllexport) int twoarrayoutmemory(int* &arrays, int first_length,int second_length);
extern "c" __declspec(dllexport) int twoarrayinmemory(int** &arrays, int &first_length,int &second_length);
extern "c" __declspec(dllexport) int twoarrayinmemoryfree(int**& arrays,int first_length);
//實現:
int add(int a, int b)
extern "c" __declspec(dllexport) int onearrayoutmemory(int* arrays, int length)
return 0;
}extern "c" __declspec(dllexport) int onearrayinmemory(int* &arrays, int& length)
return 0;
}extern "c" __declspec(dllexport) int onearrayinmemoryfree(int*& arrays)
return 0;
}extern "c" __declspec(dllexport) int twoarrayoutmemory(int* &arrays, int first_length, int second_length)
} return 0;
}extern "c" __declspec(dllexport) int twoarrayinmemory(int** &arrays, int& first_length, int& second_length)
} return 0;
}extern "c" __declspec(dllexport) int twoarrayinmemoryfree(int**& arrays, int first_length) }
return 0;
}
主要的c#呼叫匯出:
[dllimport("dlltest.dll", entrypoint = "add", exactspelling = false, callingconvention = callingconvention.cdecl)]
public extern static int add(int a, int b);
[dllimport("dlltest.dll", entrypoint = "onearrayoutmemory", exactspelling = false, callingconvention = callingconvention.cdecl)]
public extern static int onearrayoutmemory(intptr arrays, int length);
[dllimport("dlltest.dll", entrypoint = "onearrayinmemoryfree", exactspelling = false, callingconvention = callingconvention.cdecl)]
public extern static int onearrayinmemoryfree(ref intptr arrays);
[dllimport("dlltest.dll", entrypoint = "onearrayinmemory", exactspelling = false, callingconvention = callingconvention.cdecl)]
public extern static int onearrayinmemory(ref intptr arrays, ref int length);
[dllimport("dlltest.dll", entrypoint = "twoarrayoutmemory", exactspelling = false, callingconvention = callingconvention.cdecl)]
public extern static int twoarrayoutmemory(ref intptr arrays, int first_length, int second_length);
[dllimport("dlltest.dll", entrypoint = "twoarrayinmemory", exactspelling = false, callingconvention = callingconvention.cdecl)]
public extern static int twoarrayinmemory(ref intptr arrays, ref int first_length, ref int second_length);
[dllimport("dlltest.dll", entrypoint = "twoarrayinmemoryfree", exactspelling = false, callingconvention = callingconvention.cdecl)]
public extern static int twoarrayinmemoryfree(ref intptr arrays, int first_length);
c#呼叫c++相關 首先是確定記憶體是誰來分配,一般推薦c#進行記憶體分配(個人理解,因為若c++分配,c#雖然不需要去關注具體記憶體,但是這種情況下c++更容易出現記憶體洩漏問題,在c#上進行管理應該更好一點)。
c#呼叫**:
int count = 10;
int test = add(5, 10);
//一維陣列 c#申請記憶體
intptr dataone = marshal.allochglobal(marshal.sizeof(typeof(int)) * count);
onearrayoutmemory(dataone, count);
for (int i = 0; i < count; ++i)
//一維陣列 c++申請記憶體
intptr dataonecpp = intptr.zero;
int length = 0;
onearrayinmemory(ref dataonecpp, ref length);
for (int i = 0; i < count; ++i)
onearrayinmemoryfree(ref dataonecpp);
//二維陣列 c++申請記憶體
int first = 0;
int second = 0;
intptr dataintwo = intptr.zero;
twoarrayinmemory(ref dataintwo, ref first, ref second);
for (int i = 0; i < first; ++i)
}//二維陣列 c#申請記憶體
int firstlength = 10;
int seconflength = 9;
intptr datatwo = marshal.allochglobal(marshal.sizeof(typeof(int)) * firstlength * seconflength);
twoarrayoutmemory(ref datatwo, firstlength, seconflength);
for (int i = 0; i < firstlength; ++i)
console.writeline("");
}
注意:在c#分配的二維陣列,只是簡單從一維陣列模擬出來的(個人理解,一般很少出現c#分配二維陣列既指標list方式的情)。
其他的 都已實現。
c 二維陣列指標
定義指標指向二維陣列 為了方便根據使用者輸入動態定義二維陣列的行和列,引入變數rowsnum 行 colsnum 列 以定義 行 列的二維陣列為例,int rowsnum 4 int colsnum 5 float a new float rowsnum for int i 0 i rowsnum ...
關於C 二維指標
概括的說,指標其實就是可變陣列的首位址,說是可變陣列,是 指其包含內容的數量的可變的,並且是可動態申請和釋放的,從而充 分節約寶貴的記憶體資源。我一向喜歡一維陣列,除非萬不得已,我一 般是不用二維陣列的,多維的則更是很少涉足了。因為一維簡單,容 易理解,而用指標指向的多維陣列就具有相當的複雜性了,也...
C 二維陣列指標
概括的說,指標其實就是可變陣列的首位址,說是可變陣列,是 指其包含內容的數量的可變的,並且是可動態申請和釋放的,從而充 分節約寶貴的記憶體資源。我一向喜歡一維陣列,除非萬不得已,我一 般是不用二維陣列的,多維的則更是很少涉足了。因為一維簡單,容 易理解,而用指標指向的多維陣列就具有相當的複雜性了,也...