passing a int[,] to dllImport

Hello, I will just jump right into my code:

on the c++ end:

#include <iostream> using namespace std; extern "C" { __declspec(dllexport) void testFunction(int* someArray) { cout << "hey, I write something from the c++ dll :)" << endl; someArray[0] = 11; } __declspec(dllexport) void testFunction2(int** someArray) { someArray[0][0] = 1444; } } 

my C# code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace cppDLL { class Program { [DllImport("myCppDLL.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void testFunction(int[] array); [DllImport("myCppDLL.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void testFunction2(int[,] array); static void Main(string[] args) { int[] testData = {1,2,3,4,5}; int[,] testData2 = {{1,2},{1,3}}; testFunction(testData); testFunction2(testData2); Console.WriteLine(testData2[0, 0]); Console.WriteLine(testData[0]); Console.ReadKey(); } } } 

testFunction works fine and the dll has acess to the memory passed to it, because it is an int[]. However my second function (testFunction2), which passes a int[,], won't work and throws a AccessViolationException. Can I only use normal arrays int the dllImport?

Thank you for your answers!

by Hate4Fun via /r/csharp

Leave a Reply