将C ++ / CLI中的int数组返回到c#.NET [英] Returning an int array in C++/CLI to c# .NET

查看:123
本文介绍了将C ++ / CLI中的int数组返回到c#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++ / CLI包装器从c#.NET调用c ++库。尽管此特定代码有效,但我怀疑我在内存方面做错了什么。 (在连续运行此代码约20次后,我遇到了问题。)

I'm using a C++/CLI wrapper to call a c++ library from c# .NET. While this particular code "works," I suspect that I'm doing something wrong with respect to memory. (I run into problems after running this code about 20 times in a row.)

c#面:

public void ExportModelToImage(int[] myImage, int imageWidth, int imageHeight)
{
    View.ExportModelToImage(ref myImage, imageWidth, imageHeight);
}

C ++ / CLI端:

C++/CLI side:

void ExportModelToImage(array<int>^% myImage, int imageWidth, int imageHeight)
{
    if (myView().IsNull())
    {
        return;
    }
    myView()->Redraw();
    Image_PixMap theImage;
    myView()->ToPixMap(theImage, imageWidth, imageHeight);

    const int totalBytes = imageWidth * imageHeight;
    int byteIndex = 0;      
    Standard_Integer si = 0;
    Quantity_Color aColor;
    Quantity_Parameter aDummy;
    for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
    {
        for (Standard_Size aCol = 0; aCol < theImage.SizeX(); ++aCol) 
        {
            aColor = theImage.PixelColor((Standard_Integer )aCol, (Standard_Integer )aRow, aDummy);
            aColor.Color2argb(aColor, si);
            myImage[byteIndex] = (int) si;
            byteIndex++; 
            if (byteIndex > totalBytes) return;
         }
    }
}

理想情况下,如果ExportModelToImage()返回一个int数组,而不是按引用返回,但是我在找出正确的方法在C ++ / CLI中遇到问题。任何建议将不胜感激。谢谢!

Ideally, I would prefer if ExportModelToImage() returned an int array instead of returning by reference, but I've had problems figuring out the correct way to do that in C++/CLI. Any suggestions would be greatly appreciated. Thanks!

推荐答案

要返回int数组,请具有 array< int> ^ 作为返回类型,并使用 gcnew 初始化本地变量。当您呼叫 gcnew 时,别忘了放弃 ^

To return an int array, have array<int>^ as your return type, and initialize your local variable with gcnew. Don't forget to leave off the ^ when you call gcnew.

array<int>^ ExportModelToImage(int imageWidth, int imageHeight)
{
    array<int>^ result = gcnew array<int>(imageWidth * imageHeight);

    if (myView().IsNull())
    {
        return nullptr;
        // could also return a zero-length array, or the current 
        // result (which would be an all-black image).
    }
    myView()->Redraw();
    Image_PixMap theImage;
    myView()->ToPixMap(theImage, imageWidth, imageHeight);

    int byteIndex = 0;      
    Standard_Integer si = 0;
    Quantity_Color aColor;
    Quantity_Parameter aDummy;
    for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
    {
        for (Standard_Size aCol = 0; aCol < theImage.SizeX(); ++aCol) 
        {
            aColor = theImage.PixelColor((Standard_Integer )aCol, (Standard_Integer )aRow, aDummy);
            aColor.Color2argb(aColor, si);
            result[byteIndex] = (int) si;
            byteIndex++; 
        }
    }

    return result;
}

现在,也就是说,您还可以在这里进行其他操作。特别是,您可能想构造某种.Net图像类型并将其返回,而不是返回整数数组。

Now, that said, there are other possibilities you could do here. In particular, you may want to construct a .Net image type of some sort and return that, rather than returning an array of integers.

这篇关于将C ++ / CLI中的int数组返回到c#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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