how to set an object into C++ from C# -
i want configure c++ project c#.
e.g: if have class in c#:
public class person { public person(){} public string firstname {get; set;} public string lastname {get; set;} public int age {get; set;} }
then have list of persons:
person per1 = new person(); per1.firstname = "per1"; per1.lastname = "last"; per1.age = 20; person per2 = new person(); per2.firstname = "per2"; per2.lastname = "last2"; per2.age = 21;
then have:
list<person> persons = new list(); persons.add(per1); persons.add(per2);
my question how can pass '
persons
' list in c++ source.
a sample appreciated!
thanks in advance.
you cannot pass list<>
unmanaged c++, since has no access clr , wouldn't know it.
what can define structure in c++ matches layout of c# class , pass exported c++ function expects array. depending on how control have on c# , c++ definitions, there things can make life easier on yourself:
first, define c# type interop in mind:
// in c#: [structlayout(layoutkind.sequential, charset = charset.unicode)] public struct person { [marshalas(unmanagedtype.lpwstr)] public string firstname; [marshalas(unmanagedtype.lpwstr)] public string lastname; public int age; } // in c++ typedef struct tagperson { lpwstr firstname; lpwstr lastname; long age; } person;
second, since c/c++ arrays aren't managed you'll need other way define how big is; easiest option pass second parameter c++ function length.
// in c++ extern "c" __declspec( dllexport ) void myfunction(long count, person[] people); // in c# [dllimport("mydll.dll")] public static extern void myfunction( int count, [marshalas(unmanagedtype.lparray, sizeparamindex = 0)] person[] people);
then can call method in c#; if have populated list<person>
this:
var personsarray = persons.toarray(); nativemethods.myfunction(personsarray.length, personsarray);
Comments
Post a Comment