Returning an int array in C++/CLI to c# .NET

百般思念 提交于 2019-12-24 11:26:43

问题


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# side:

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

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;
         }
    }
}

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!


回答1:


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;
}

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.



来源:https://stackoverflow.com/questions/27492520/returning-an-int-array-in-c-cli-to-c-sharp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!