本文介绍了从C ++调用自引用结构的P的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是C ++中的自引用结构

Following is a self referential struct from C++

typedef struct _FCV
{
  unsigned long    ulID;
  unsigned long    ulVersion;
  unsigned long    ulStatus;
  unsigned long    ulSize;
  struct _FCV*     pNext;
} FCV;

我需要使用PInvoke转换为C#结构,我应该声明"pNext"是什么?谢谢.

I need to use PInvoke to translate to C# struct,What is the "pNext" i should declare?Thank you.

推荐答案

您也许已经达到了p/invoke并不是最佳的工具.这里的复杂性可能会使C ++/CLI层成为更具吸引力的选择.

You have perhaps reached the point where p/invoke is not the best tool for the job. The complexity here may make a C++/CLI layer a more attractive option.

使用p/调用,您需要将pNext字段声明为IntPtr.然后,您需要为链表中的每个项目填充一个结构实例.最后,您需要遍历分配给pNext的列表.这将要求您使用GCHandle.Alloc固定每个结构,然后使用AddrOfPinnedObject获取固定的地址.调用完成后,您需要销毁所有GCHandle对象以取消固定结构.

With p/invoke you'd need to declare the pNext field as IntPtr. Then you'd need to populate one instance of the struct for each item in the linked list. Finally you'd need to walk through the list assigning to pNext. That will require you to pin each struct with GCHandle.Alloc and then get the pinned address with AddrOfPinnedObject. Once the call has been made you then need to destroy all the GCHandle objects to un-pin the structs.

因此可以这样做,但是代码可能相当笨拙,并且效率可能不高.您应该认真考虑使用C ++/CLI.

So it's possible to do, but the code may be rather unwieldy, and may not be particularly efficient. You should seriously consider C++/CLI instead.

这篇关于从C ++调用自引用结构的P的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:40