我正在尝试使用结构数组将数据传递给我的 C# ui 和我的 C dll。更复杂的事情是该结构包含另一个结构数组。我已经想出了如何使用 pinvoke 来实现更简单的东西,但是如何声明嵌入式结构,以及如何传递它们?
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class csForm {
public int endDate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public char[] formId;
}
[DllImport("myDll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern void fillForm([In, Out] csForm data, 5);
// c code
typedef struct s_ptxRow {
int ptxNumber;
char primitive[128];
int primitiveParams[128];
} ptxRow;
typedef struct s_workSpace{
char formId[128];
int endDate;
ptxRow PtxRow[128];
} cForm;
extern "C" __declspec(dllexport) fillForm(cForm csForm[], interface csFormCount)
{
for (int i = 0, j = 0; i < csFormCount; ++i)
{
j = int / 2;
csForm[i].endDate = i;
strcpy(csForm[i].formId, "formId here");
csForm[i].PtxRow[j].ptxNumber = i;
csForm[i].PtxRow[j].primitiveParams[i] = i;
strcpy(csForm[i].PtxRow[j].primitive, "someText");
}
}