管理与非管理之间的传递结构 [英] Passing structure between manage and unmanaged

查看:82
本文介绍了管理与非管理之间的传递结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个结构包含另一个结构指针,该结构从C#传递到非托管C ++/COM.将数组分配给结构内的结构指针时出现错误.我不确定,这是否受支持,或者我做错了什么? 这将是很大的帮助,有人可以为我指明正确的方向.

I'm trying to pass a structure that has another structure pointer inside to unmanaged C++/COM from C#. I'm getting an error when assigning array to structure pointer inside a structure. I'm not sure, is this something supported or I'm doing something wrong? It would be great help, somebody could point me to right direction.

C++/MFC - win32-----------------[export]typedef struct Person{  BSTR personName;  BSTR city;}[export]typedef struct Persons{   Person *pPersonList;  //I have other information in structure that I need receive as a group of information.}BOOL GetPersons(Person *pPerson);BOOL GetPersonList(Persons *pPersonList); c#---Person[10] personList = new Person[10];for(iint i=0;i<10;i++){  Person person = new Person();  person.personName = "Name " + i;  personList[i] = person;}GetPersons(ref personList); //worksPersons persons = new Persons();persons.pPersonList = personList; //ErrorGetPersonList(ref persons);Error:-----Cannont implicitly convert type Person[] to 'System.IntPtr' 

when I see structure definition in c#, this is how it shows.typedef struct Persons{   IntPtr pPersonList; }

我该如何解决?

谢谢.

推荐答案

您不能在C#中使用IntPtr数据. IntPtr仅用于传递指向分配的内存的指针.并在非托管代码中使用.有关最新示例,请查看Codeproject.com上的P-Invoke文章.
You can't use IntPtr data in C#. IntPtr is only used to pass around a pointer to memory that is allocated  and used in unmanaged code. Check the P-Invoke articles on Codeproject.com for the latest examples.


这篇关于管理与非管理之间的传递结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆