簡體   English   中英

將C ++數組傳遞給C#函數

[英]Passing a C++ array to a C# function

我正在嘗試使用C#庫輕松地將原始像素數組轉換為PNG文件。 我使用C ++和OpenGL編寫了初始程序,所以最好的方法是使用COM將我的C ++像素數組傳遞給C#dll,並使C#dll將它們另存為PNG文件。 我可以輕松地將值傳遞到C#中,但無法快速找出如何將非托管C ++數組傳遞給C#。 我讀過您可以使用SAFEARRAYS或進行某種編組和拆組。 無論如何,我不確定它們。 這是我的C#代碼,

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PNG_Creator_DLL
{
[ComVisible(true)]
public interface IPicturesAndMovies
{
    //int Add(int a, int b);
    int CreatePNG(UInt16[] imagery_data);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class PNG_Create_Class: IPicturesAndMovies
{
    public int CreatePNG(UInt16[] imagery_data)
    {


        //create the images directory
        Directory.CreateDirectory("images");

        String fileName = "images\\PNG_test_nmr" + "_0000.png";
        var pngStream = new FileStream(fileName, FileMode.Create);

        readImage(imagery_data, pngStream, 384, 288);  //create the png frame
        pngStream.Close();
        fileName = "images\\PNG_test_nmr" + "_" + "0001" + ".png";
        pngStream = new FileStream(fileName, FileMode.Create);

        pngStream.Close();
        File.Delete(fileName);


        return 0;
    }

    private Boolean readImage(UInt16[] imagery_data, Stream pngStream, Int32 width, Int32 height)
    {


        var bmp = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray16, null);
        Int32 bytesPerPixel = bmp.Format.BitsPerPixel / 8;
        Int32Rect drawRegionRect = new Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight);


        Int32 stride = bmp.PixelWidth * bytesPerPixel;
        bmp.WritePixels(drawRegionRect, imagery_data, stride, 0);

        PngBitmapEncoder enc = new PngBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bmp));
        enc.Save(pngStream);

        return true;
    }
 }
}

這是我的C ++代碼,它使用COM並嘗試將數組傳遞給C#dll

#include "stdafx.h"
#include <Windows.h>
#import "..\x64\Release\dllFiles\PNG_Creator_DLL.tlb" no_namespace

int main()
{
unsigned short* imagery_data = new unsigned short[384 * 288];
unsigned short temp_sum = 0;
for (int index_row = 0; index_row < 288; index_row++)
{
    temp_sum = 0;
    for (int index_col = 0; index_col < 384; index_col++)
    {

        imagery_data[index_row * 384 + index_col] = temp_sum;
        temp_sum = (unsigned short)(temp_sum + 160);
    }
}

CoInitialize(NULL);

IPicturesAndMoviesPtr obj;
obj.CreateInstance(__uuidof(PNG_Create_Class));
printf("Create      = %d\n", obj->CreatePNG(imagery_data));

CoUninitialize();
return 0;
}

我在printf(“ Create =%d \\ n”,obj-> CreatePNG(imagery_data))處收到C2664編譯器錯誤;

它說'long IPicturesAndMovies :: CreatePNG(SAFEARRAY *)':無法將參數1從'unsigned short *'轉換為'SAFEARRAY *'

關於如何解決它的任何想法?

嘗試如下所述。 可能有效

//YOUR ARRAY NEEDS TO BE CONVERTED TO SAFE ARRAY
unsigned short* imagery_data = new unsigned short[384 * 288];

//DECLARE A SAFE ARRAY POINTER
SAFEARRAY *psa;  


//AS THE REQUIRED IS UInt16[] - USE VT_UI2 
//LOWER BOUND  = 0
//UPPER BOUND = 110592 (384 * 288)
psa = SafeArrayCreateVector(VT_UI2, 0, 110592);  //REFER DOCUMENTATION

unsigned short *pData;  
HRESULT hr = SafeArrayAccessData(psa, (void **)&pData); //REFER DOCUMENTATION
memcpy(pData, imagery_data, 110592*sizeof(unsigned short));  
SafeArrayUnaccessData(psa);//REFER DOCUMENTATION

然后使用psa通過。

使用完成后,請致電SafeArrayDestroy進行清理。

SafeArrayDestroy(psa);

上面的代碼從下面的鏈接中抬起並縮短。

https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/dotnet/how-to-marshal-a-safearray-for-adonet-cpp-cli.md

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM