How to marshal C++ array to C# without specifying a buffer size? -


i have c++ function provides array c# method. don't know how many elements i'll have in return. want pass array (without specifying element count) c++ function , filled.

my try far:

c++ code

unsigned int lib_infos(     const char *code,     char **err,     int err_len,     char **linkinfo,     unsigned int* linkscount); 

c# code

[dllimport("mylib.dll", callingconvention = callingconvention.cdecl)] private static extern uint lib_infos(     [marshalas(unmanagedtype.lpstr)] string code,     [marshalas(unmanagedtype.lpstr)] stringbuilder err,     int err_len,     [marshalas(unmanagedtype.lparray,sizeparamindex=1)] string[] linkinfo,     out uint linkscount); 

it's impossible this. c++ code cannot magically know size of array- or magically expand array match.

there must communication protocol array size- either tell size of array , ask n items, effectively, or tells there n items , provide array of size.

you cannot pass arrays , forth without being able determine it's size.


Comments