0

I want to configure a C++ project from C#.

E.g: If I have this class in C#:

public class Person
{
    public Person(){}

    public string FirstName
    {get; set;}

    public string LastName
    {get; set;}

    public int Age
    {get; set;}
}

Then I have a 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 I have:

List<Person> persons = new List();
persons.Add(per1);
persons.Add(per2);

My question is how can I pass that '

persons

' list in a C++ source.

A sample is much appreciated!

Thanks in advance.

7
  • Are you looking to pass an object created in C# as a parameter to some function in C++? Is that the question. Can you please clarify. Commented Mar 11, 2012 at 1:12
  • @Gangadhar yes, I am looking a way to pass an generic List object (or array of objects) in C# to a function in C++ (not C#). Commented Mar 11, 2012 at 1:13
  • Have you looked at this? It may help you better frame your question. codeguru.com/forum/showthread.php?t=354807 Commented Mar 11, 2012 at 1:17
  • So you want to call the methods of managed class "List" in C++ ? Commented Mar 11, 2012 at 1:24
  • @JamesBlack that is a nice reference as well. thanks Commented Mar 11, 2012 at 9:14

1 Answer 1

1

You cannot pass a List<> to unmanaged C++, since it has no access to the CLR and wouldn't know what to do with it.

What you can do is define a structure in C++ that matches the layout of your C# class and pass that to an exported C++ function that expects an array. Depending on how much control you have over the C# and C++ definitions, there are some things you can do to make life easier on yourself:

First, define your C# type with 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 some other way to define how big it is; the easiest option is pass a second parameter to your C++ function that is the 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 you can simply call this method in C#; if you already have your populated List<Person> you would do this:

var personsArray = persons.ToArray();
NativeMethods.MyFunction(personsArray.Length, personsArray);
Sign up to request clarification or add additional context in comments.

8 Comments

I am greatly appreciated with this post. Very nice! I wonder what is this method called? Is it C++/CLI or another name? I will try it and come back to this post soon! thanks again
This is P/Invoke. C++/CLI is a different (similar) concept; if your C++ code is written in managed C++ (C++ with direct references to the .NET CLR) then you don't need to do any of this, you can simply reference it exactly as you would an assembly written in VB.NET or C# .
also don't forget to accept an answer if its correct, or upvote them if they are helpful; user voting is how this site functions.
Thanks a lot. I am looking into it. Is it a lot of time to create a sample like the P/invoke above with C++/CLI? If not, could you please help me to create a sample one as well in C++/CLI? I will compare them and to see which one is better? thanks in advance
I haven't done any C++/CLI personally so I'm probably the wrong person to ask; the Framework classes are identical (so List has Add/Remove/ Contains/etc) but creating objects and such is quite different. A quick google turns up this: functionx.com/vccli/collections/bicc.htm
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.