How can I list the list <int>?
I have a dll in C ++, it returns a list, I want to use it in my C # application as a list
[DllImport("TaskLib.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern List<int> GetProcessesID();
public static List<int> GetID()
{
List<int> processes = GetProcessesID();//It is impossible to pack a "return value": The basic types can not be packed
//...
}
+3
2 answers
Per Jared Par:
Usually, generators are not supported in any interaction scenario. Both PInvoke and COM Interop will fail if you try to marshal a common type or value. Therefore, I would expect Marshal.SizeOf to be unverified or not supported for this scenario, as this is a specific Marshal function.
+3
one of the possible scenarios
C ++ side
struct ArrayStruct
{
int myarray[2048];
int length;
};
extern "C" __declspec(dllexport) void GetArray(ArrayStruct* a)
{
a->length = 10;
for(int i=0; i<a->length; i++)
a->myarray[i] = i;
}
C # side
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ArrayStruct
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
public int[] myarray;
public int length;
}
[DllImport("TaskLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void GetArray(ref ArrayStruct a);
public void foo()
{
ArrayStruct a = new ArrayStruct();
GetArray(ref a);
}
+1