With great help of the stackoverflow community, I've managed to call a native DLL function. However, I can't modify the values of ID or intersects array. No matter what I do with it on the DLL side, the old value remains. It seems read-only.
Here are some code fragments:
C++ struct:
typedef struct _Face { int ID; int intersects[625]; } Face; C# mapping:
[StructLayout(LayoutKind.Sequential)] public struct Face { public int ID; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 625)] public int[] intersects; } C++ method (type set to DLL in VS2010):
extern "C" int __declspec(dllexport) __stdcall solve(Face *faces, int n){ for(int i =0; i<n; i++){ for(int r=0; r<625; r++){ faces[i].intersects[r] = 333; faces[i].ID = 666; } } C# method signature:
[DllImport("lib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] public static extern int solve(Face[] faces, int len); C# method invocation:
Face[] faces = new Face[10]; faces[0].intersects = new int[625]; faces[0].ID = -1; //.. and add 9 more .. solve(faces, faces.Length); // faces[0].ID still equals -1 and not 666 Kindest regards, e.
SetLastErroron your functions. That is for Windows API functions only. That same advice still applies. The CharSet and CallingConvention settings are option in C# since you are using the C# defaults. I'd remove them too since they just add clutter.