Hello friend,
The following code will not run properly.
C++ codes:
// mytest.cpp
#include "stdafx.h"
#include
#include
extern "C"{
_declspec(dllexport) void _setv(char ***s, int row, int column){
for (int i = 0; i < row;i++){
for (int j = 0; j < column;j++)
{
strcat(s[i][j],"The value in here.");
}
}
}
}
C# codes:
class Program
{
[DllImport("mytest.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void _setv(string[,] s, int row, int column);
static void Main(string[] args)
{
string[,] _arr = new string[2, 2]{{"",""},{"",""}};
_setv(_arr, 2, 2);
}
}
Thank.

VulpesPosted Mar 20, 2015, 7:43 PM
There are a couple of other points to note:
1. As the unmanaged function is filling the array and then returning it to managed code, a large enough buffer needs to be created for each string element including the terminating null character - I've used 80 characters.
2. Normally, you'd use StringBuilder instead of string for a mutable string but an array of StringBuilders doesn't work and therefore you have to use a string array. However, you need to specify the [In, Out] attributes for this to work.
3. The marshaler needs to know how big the string array is and I've specified 4 as there are 2 x 2 elements. The 'row' parameter has been changed to 'size' to reflect this.
So, here's the revised code:
// C++ code
// C# code
As expected, the output is:
Ken HPosted Mar 22, 2015, 11:39 AM
VulpesPosted Mar 21, 2015, 5:52 PM
Ken HPosted Mar 20, 2015, 9:31 PM
Thank for you Vulpes. :)