将指向结构数组指针的指针从 C# 传递给 C++ [英] Passing pointer to pointer of array of structure from C# to C++

查看:113
本文介绍了将指向结构数组指针的指针从 C# 传递给 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将指向结构数组指针的指针从 C# 传递给 C++.使用下面的代码,我只得到了 C++ 中的第一个元素,没有传递数组的第二个和第三个元素.为什么?此外,尝试使用 StructureToPtr 但没有帮助.我做错了什么?

I want to pass pointer to pointer of an array of structure from C# to C++. With following code I only get the first element in c++, second and third element of the array is not passed. Why? Also, tried using StructureToPtr but didn't help. What I am doing wrong?

C++ 代码

 struct structure 
{
   short ps;

};
   __declspec(dllexport)short Testmethod(structure** aa)
    {
     if (aa!= 0 && aa[0]->ps == 26 && aa[1]->ps == 27)
     {          
        return 1;
      }    
      return  0;
    }

C# 代码

[DllImport("Wrapper.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern short Testmethod(ref IntPtr a);

     [StructLayout(LayoutKind.Sequential)]           
     public struct SampleStructure
    {
       public short ps;

    };

 SampleStructure[] data= new SampleStructure[3] { new SampleStructure { ps = 26 }, new SampleStructure { ps = 27 }, new SampleStructure { ps = 28 } };
                        GCHandle gch = GCHandle.Alloc(data, GCHandleType.Pinned);
                        IntPtr ptr = gch.AddrOfPinnedObject();
                        ret = Testmethod(ref ptr);

我无法更改 C++ 代码,我无权访问它.如果将 C++ 代码更改为以下任一方式,则它可以工作.如何在不更改 C++ 代码的情况下实现它?更新:

I can't change c++ code, I don't have access to it. If change c++ code as either one of the following ways, it works. How can I make it without changing the c++ code? Updates:

方式 1: 如果我更改了 C++ 代码,就可以工作.但我无法更改 C++ 代码.这不是我的代码.

Way 1: works if I change the c++ code. But I can't change the C++ code. It's not my code.

C++ 代码

__declspec(dllexport)short Testmethod(structure* aa)

C# 代码

            fixed (SampleStructure* pArray = dataInformation)
            {
                ret = Testmethod(pArray);
            }

方式 2: 如果我更改了 C++ 代码,就可以工作.但我无法更改 C++ 代码.这不是我的代码.

Way 2: works if I change the c++ code. But I can't change the C++ code. It's not my code.

C++ 代码

__declspec(dllexport)short Testmethod(structure Getcsharpval[], int size)

c# 代码

 public unsafe static extern short Testmethod([MarshalAs(UnmanagedType.LPArray,SizeParamIndex=3)] SampleStructure[] array);
 SampleStructure[] data= new SampleStructure[3] { new SampleStructure { ps = 26 }, new SampleStructure { ps = 27 }, new SampleStructure { ps = 28 } };
 ret = Testmethod( data);

推荐答案

好的,我通过使用以下代码创建指向结构指针的指针来解决.

Okay, I solved by creating the pointer to pointers of structures using the following code.

    [DllImport("Wrapper.dll", CallingConvention = CallingConvention.StdCall)]
    public unsafe static extern short Testmethod (SampleStructure** passstructurepointer);

     unsafe
            {
                fixed(SampleStructure** p2p = new SampleStructure*[3])
                {
                    for (short i = 0; i < 3; i++)
                    {
                        var a = stackalloc SampleStructure[1];
                        a->ps = i;
                        p2p[i] = a;
                    }
                    var s= Testmethod(p2p);
                }
            }

这篇关于将指向结构数组指针的指针从 C# 传递给 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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