c# - Cannot marshal 'parameter #': Internal limitation: structure is too complex or too large -
i have dll developed in c++. need use c#.
[structlayout(layoutkind.sequential, size = 205004, pack = 1)] private struct mylist { public uint32 count; [marshalas(unmanagedtype.byvalarray, sizeconst = 100)] public mystruct[] info; };
"mystruct" has of size 2050. calling dll method as
[dllimport("dllname.dll" callingconvention = callingconvention.cdecl)] private static extern uint32 getmylist(out mylist list);
i error when call dll method. "cannot marshal 'parameter #1': internal limitation: structure complex or large."
do 1 have solution not changing c++ dll parts?
as error says, structure large marshalled way. have find approach.
it make more sense, in view, return structs 1 @ time. avoid needing hard code upper limit of there being no more 100 structs in list.
so write this:
c++
int getlistcount() { return count; } int getlistitem(int index, mystruct* item) { if (index < 0) return -1; if (index >= count) return -1; *item = items[index]; return 0; }
c#
[dllimport("dllname.dll" callingconvention = callingconvention.cdecl)] private static extern int getlistcount(); [dllimport("dllname.dll" callingconvention = callingconvention.cdecl)] private static extern int getlistitem(int index, out mystruct item); .... int count = getlistcount(); mystruct[] items = new mystruct[count]; (int index = 0; index < count; index++) { int retval = getlistitem(out items[index]); if (retval != 0) // handle error }
if cannot change dll faced performing marshalling hand. go this:
[dllimport("dllname.dll" callingconvention = callingconvention.cdecl)] private static extern uint32 getmylist(intptr listptr); .... intptr listptr = marshal.allochglobal(205004); // argh! magic constant alert try { int retval = getmylist(listptr); // presumably expected retval int count = marshal.readint32(listptr); mystruct[] items = new mystruct[count]; (int index = 0; index < count; index++) { intptr itemptr = listptr + 4 + index*2050; // more magic constants! items[index] = (mystruct)marshal.ptrtostructure(itemptr, typeof(mystruct)); } } { marshal.freehglobal(listptr); }
you might prefer use marshal.sizeof
rather magic constants.
Comments
Post a Comment