1

I am working on project that required to pass array of objects from c# to c++ by reference and when I pass one object by reference it return by the new value successfully but when I pass an array it return with the old value so what is wrong with my code?

C++ code

extern "C" __declspec(dllexport) int solve(Cell*(&xx)[5][5]) {
xx[0][0]->side = 55;
return xx[0][0]->side;
}

and C# code

internal static class UnsafeMethods
{
[DllImport(@"E:\Cs\4th Year\HPC\Parallel_calculations\Debug\Parallel_calculations.dll", EntryPoint = "solve", CallingConvention = CallingConvention.Cdecl)]
public static extern int solve([MarshalAs(UnmanagedType.SafeArray)] ref Cell[,] x);
}

 Cell[,] arr = new Cell[5, 5];
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            for (int y = 0; y < arr.GetLength(1); y++)
            {
                arr[i, y] = new Cell(0, 0, 0, false, false, false, 50);
            }
        }
        arr[0, 0].side = 4;
        int x = UnsafeMethods.solve(ref arr);
        Console.WriteLine(x + " " + arr[0, 0].side);

x is with new value but arr[0,0].side return with old value

Dee Ess
  • 52
  • 9
Ahmed Mohamed
  • 231
  • 4
  • 15
  • Possible duplicate of [Pass multi - dimensional array from managed code to unmanaged code](http://stackoverflow.com/questions/7153521/pass-multi-dimensional-array-from-managed-code-to-unmanaged-code) – Eugene Podskal Dec 22 '16 at 18:53
  • no i can send it arleady but without reference ! – Ahmed Mohamed Dec 22 '16 at 18:58
  • I am not sure that one can correctly pass multidimensional array to native function other then by how it is described in the linked question, but I may be wrong. In any case, arrays by themselves are passed as [In] parameters - https://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.110).aspx. So you should at least try to add [InOut] attribute on the array parameter. If that won't work, then you should try the proposed solution and then manually reconstruct the array - http://stackoverflow.com/questions/6747112/getting-array-of-struct-from-intptr – Eugene Podskal Dec 22 '16 at 19:05
  • till now no one of the two answers solve my problem and i can only pass the array by value !, any help – Ahmed Mohamed Dec 22 '16 at 20:02
  • Another problem with your code is that you specify UnmanagedType.SafeArray, but the C++ code is using a reference to an array (basically a pointer to pointer to the first element of the array). The correct C++ type to use is the SAFEARRAY structure. Otherwise, leave the C++ code as is, and specify UnmanagedType.LPArray in C#. Also, as the other poster mentioned, arrays are always marshaled as one-dimensional between managed and unmanaged code. – user1610015 Dec 22 '16 at 21:10
  • ok i try UnmanagedType.LPArray but i get another exception and about arrays are always marshaled as one-dimensional between managed and unmanaged code , how i can send 2d int array from c# to c++ successfully ,, the whole problem when i pass 2d array of cell object – Ahmed Mohamed Dec 22 '16 at 21:14
  • and when i don't use marchale it doesn't sent to c++ function – Ahmed Mohamed Dec 22 '16 at 21:16
  • You will likey need to create a C++/cli assembly to bridge the gap. You can't do it with straight pinvoke – Scott Chamberlain Dec 22 '16 at 21:25
  • what do you mean by " create a C++/cli assembly to bridge the gap" @ScottChamberlain – Ahmed Mohamed Dec 22 '16 at 21:27
  • This, it lets you combine native and managed code https://msdn.microsoft.com/en-us/library/68td296t.aspx – Scott Chamberlain Dec 22 '16 at 22:02

0 Answers0