如何设置一个对象到C ++从C# [英] how to set an object into C++ from C#
问题描述
我想从C#配置C ++项目。
例如:如果我在C#中有这个类:
public class Person
{
public Person(){}
public string FirstName
{get; set;}
public string LastName
{get; set;}
public int Age
{get; set;}
}
然后我有一个人的列表:
Person per1 = new Person();
per1.FirstName =Per1;
per1.LastName =Last;
per1.Age = 20;
人per2 = new Person();
per2.FirstName =Per2;
per2.LastName =Last2;
per2.Age = 21;
那么我有:
列表< Person> persons = new List();
persons.Add(per1);
persons.Add(per2);
我的问题是我如何通过'
人
您不能将 List<>
传递给非托管C ++,因为它无法访问CLR,并且不知道该怎么处理。
您可以做的是在C ++中定义一个与C#类的布局相匹配的结构,并将其传递给一个导出的C ++函数,该函数需要一个数组。根据你对C#和C ++定义的控制程度,你可以做一些事情来使自己的生活更容易:
首先,定义你的C#类型Interop in mind:
//在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;
}
//在C ++中
typedef struct tagPerson
{
LPWSTR firstname;
LPWSTR lastname;
长年龄;
}人;
其次,由于C / C ++数组不受管理,您需要一些其他方法来定义大是最简单的选项是将第二个参数传递给您的C ++函数,即长度。
//在C ++中
extern C__declspec(dllexport)void MyFunction(LONG count,Person [] people);
//在C#
[DllImport(mydll.dll)]
public static extern void MyFunction(
int count,
[MarshalAs UnmanagedType.LPArray,SizeParamIndex = 0)] Person [] people);
然后你可以在C#中调用这个方法;如果您已经有您的填充列表<人>
,您将这样做:
var personsArray = persons.ToArray();
NativeMethods.MyFunction(personsArray.Length,personsArray);
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.
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);
这篇关于如何设置一个对象到C ++从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!