且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从C ++调用自引用结构的P

更新时间:2023-02-02 23:24:40

您也许已经达到了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.