public static extern int getOpToArr( [MarshalAs(UnmanagedType.LPStr)] string myNodeGuid, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] out DN_OPstruct[] array, out int arraySizeInElements );
The problem is the second parameter. Unmanaged code cannot synthesize a managed .net array. You need to declare p / invoke as follows:
public static extern int getOpToArr( string myNodeGuid, out IntPtr arrayPtr, out int arrayLen );
Then you will need to use Marshal.PtrToStructure to marshal the elements of the array in a managed array.
IntPtr arrayPtr; int arrayLen; int retval = getOpToArr(nodeGuid, out arrayPtr, out arrayLen);
I am also a little skeptical about the properties of your structure. Why do you have setters as well as getters? It does not look like data flows in that direction. And the unmanaged code you use shows the distribution using CoTaskMemAlloc , which does not match StringToHGlobalAnsi . Therefore, despite the fact that I doubt that you should write settings and, perhaps, should delete calls to StringToHGlobalAnsi , I also suspect that you are using confusion over the dispenser used.
Please note that the code in your question does not provide any evidence of how you allocated the array that is returned to the caller. So, as far as we know, a problem may arise in this part of the code.
source share