将C#多维数组传递给C ++ [英] Passing C# Multidimensional Array to C++

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

问题描述

 //  C ++ 

命名空间
{

    公共 参考  class  Class1
    {

        公共:
            静态 无效测试( double  ** a , int  nRow, int  nCol)
            {
                           // 使用2D数组进行操作.

            }

    };

}
//  ========================= ================================  



 //  C#

      使用

       私有 无效 button1_Click(对象发​​件人,EventArgs e)
        {

             double  [,] a =   double  [ 4  3 ];
            a [ 0  0 ] =  1  ; a [ 1  0 ] =  1  ; a [ 2  0 ] =  1  ; a [ 3  0 ] =  0  ;
            a [ 0  1 ] =  2  ; a [ 1  1 ] =  1  ; a [ 2  1 ] =  1  ; a [ 3  1 ] =  1  ;
            a [ 0  2 ] =  1  ; a [ 1  2 ] =  2  ; a [ 2  2 ] =  1  ; a [ 3  2 ] =  15  ;


            不安全
            {
                已修复(双** ppa = a)// 什么正确的语法是什么?
                {

                    Class1.Test(ppa,a.GetLength( 0 ),a.GetLength( 1 ));
                }
            }

    }
//  ++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++  






C#语法fixed (double** ppa = a)在VS2010 C#中生成了以下错误消息.

错误1无法将类型double *隐式转换为double **.存在显式转换(您是否缺少演员表?)

将C#部分中生成的2D数组传递到C ++ ref类的正确方法是什么?指向本机代码中的指针的指针.您可以将二维数组转换为双锯齿数组(数组数组),然后将其转换为指向指针的指针,然后将其传递给C ++.

但是,这里一个更重要的问题是,为什么您需要完全执行此操作.您的C ++类是托管类,可以轻松地处理托管数组(包括二维数组).那么,您需要使用指针做什么呢?


>>你能告诉我如何



这是我几年前写的文章:

http://www.codeproject.com/KB/mcpp/cppcliarrays.aspx [ ^ ]

向下滚动到标题为使用本机指针直接操作CLI数组"的部分,然后向下滚动至第二小节以本地方式访问锯齿状的数组".您可能需要进行一些更改以适合您的特定需求,但是该示例将告诉您您需要做什么.


整个要点是避免在C#中执行不安全的代码.将托管类型(2-d或锯齿状数组)传递给C ++/CLI ref类. C ++/CLI ref类将其转换为本机指针类型并调用本机函数.通常就是这样.

[更新(此评论与回答的东西有点混乱!)]
-------------------------------------------------- --------------

这是一个示例,显示了如何将二维数组传递给需要指针的本地函数.

  void  Foo( double  ** nums, int  yLen)
{
     for ( int  i =  0 ; i< ; xLen; i ++)
    {
         for ( int  j =  0 ; j< ; yLen; j ++)
        {
             double  d = *( double  *)(nums [i] + j);
        }
    }
}

 int  main(...)
{
    array< double,2> ^数字=  gcnew  array< double,2>( 2  3 );
    数字[ 0  0 ] =  12  . 3 ;
    数字[ 0  1 ] =  2  . 1 ;
    数字[ 0  2 ] =  4  . 3 ;
    数字[ 1  0 ] =  23  . 2 ;
    数字[ 1  1 ] =  7  . 7 ;
    数字[ 1  2 ] =  17  . 6 ;

    pin_ptr< double> p1 =& numbers [ 0  0 ];
    pin_ptr< double> p2 =& numbers [ 1  0 ];
     double  * p3 [ 2 ];
    p3 [ 0 ] = p1;
    p3 [ 1 ] = p2;
     double  ** p4 = p3;

    Foo(p4, 2  3 );

    返回  0 ;
} 


  // C++

namespace whatever
{

    public ref class Class1
    {

        public:
            static void Test(double **a, int nRow, int nCol)
            {
                           // Do something with 2D array.

            }

    };

}
//========================================================



// C#

      using whatever

       private void button1_Click(object sender, EventArgs e)
        {

            double[,] a = new double[4, 3];
            a[0, 0] = 1; a[1, 0] = 1; a[2, 0] = 1; a[3, 0] = 0;
            a[0, 1] = 2; a[1, 1] = 1; a[2, 1] = 1; a[3, 1] = 1;
            a[0, 2] = 1; a[1, 2] = 2; a[2, 2] = 1; a[3, 2] = 15;


            unsafe
            {
                fixed (double** ppa = a) // What is the correct syntax?
                {

                    Class1.Test(ppa, a.GetLength(0), a.GetLength(1));
                }
            }

    }
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++






The C# syntax fixed (double** ppa = a) generated the following error message in VS2010 C#.

Error 1 Cannot implicitly convert type double* to double**. An explicit conversion exists (are you missing a cast?)

What is the correct way to pass the 2D array generated in the C# part over to the C++ ref class?

解决方案

Unfortunately a managed multi-dimensional array is not equivalent to a pointer to pointer in native code. You could convert your 2-dimensional array into a doubly jagged array (an array of arrays) and then convert that to a pointer to a pointer and then pass that to C++.

But a more important question here is why you need to do this at all. Your C++ class is managed and can easily and trivially handle a managed array (including a 2-d array). So what do you need to resort to pointers for?


>> Would you show me how



This is an article I wrote some years back:

http://www.codeproject.com/KB/mcpp/cppcliarrays.aspx[^]

Scroll down to the section titled "Direct manipulation of CLI arrays using native pointers", and then the 2nd sub-section "Natively accessing a jagged array". You may need to change things just a little to fit your specific needs but that example should tell you what you need to do.


The whole point is to avoid doing the unsafe code in C#. Pass the managed type (2-d or jagged array) to the C++/CLI ref class. The C++/CLI ref class converts this to the native pointer type and calls the native function. That''s how this is typically done.

[Update (this comment vs answer thing is kinda messy!)]
----------------------------------------------------------------

Here''s a sample showing how you can pass a 2-d array to a native function expecting a pointer to pointer.

void Foo(double** nums, int xLen, int yLen)
{
    for (int i=0; i<xLen; i++)
    {
        for (int j=0; j<yLen; j++)
        {
            double d = *(double*)(nums[i] + j);
        }
    }
}

int main(. . .)
{
    array<double, 2>^ numbers = gcnew array<double, 2>(2,3);
    numbers[0,0] = 12.3;
    numbers[0,1] = 2.1;
    numbers[0,2] = 4.3;
    numbers[1,0] = 23.2;
    numbers[1,1] = 7.7;
    numbers[1,2] = 17.6;

    pin_ptr<double> p1 = &numbers[0,0];
    pin_ptr<double> p2 = &numbers[1,0];
    double* p3[2];
    p3[0] = p1;
    p3[1] = p2;
    double** p4 = p3;

    Foo(p4, 2, 3);

    return 0;
}


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

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