从c ++到c#的pinvoke字节数组

时间:2017-06-25 22:12:11

标签: c# c++ pinvoke

我正在尝试将用c ++创建的字节数组移动到c#以供使用,现在我看到字节数组在c ++端是有效的,但是当我回到c#时我得到空值。

c ++代码

__declspec(dllexport) void test(unsigned char* t_memory, int* t_size)
{
    int width, height, channels_in_file;
    t_memory = stbi_load("test.png", &width, &height, &channels_in_file, 0);
    *t_size = width*height;
}

c#c​​ode

[DllImport(DllFilePath, EntryPoint = "test", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static extern int _test(ref IntPtr memory, out int size);

public static void Test()
{   
    IntPtr memory = IntPtr.Zero;
    _test(ref memory, out int size);

    byte[] memoryArray = new byte[size];

    Marshal.Copy(memory, memoryArray, 0, size);

    Bitmap bmp;
    using (var ms = new MemoryStream(memoryArray))
    {
        bmp = new Bitmap(ms);
    }

    bmp.Save("test_recreated.png");
}

2 个答案:

答案 0 :(得分:1)

C ++代码不返回数组,因为参数声明不正确。您传递指针但需要传递指针的地址。

应该像这样更改C ++代码以匹配C#代码:

__declspec(dllexport) int test(unsigned char** t_memory, int* t_size)
{
    int width, height, channels_in_file;
    *t_memory = stbi_load("test.png", &width, &height, &channels_in_file, 0);
    *t_size = width*height;
    return 0;
}

您必须传递数组的地址,而不是数组本身。请注意此更改后与size参数设置的相似性。

我还包含一个匹配C#代码的返回值。

您还需要导出解除分配器以避免泄漏此内存。

答案 1 :(得分:-2)

请尝试以下代码。一旦存在,方法t_size将不存在。因此必须在调用方法中分配内存。你的形象有多大大小可能必须是长而不是int。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string DllFilePath = @"c:\temp";

        [DllImport(DllFilePath, EntryPoint = "test", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        private static extern int _test(ref IntPtr memory, ref IntPtr size);

        static void Main(string[] args)
        {
        }

        public static void Test()
        {   
            IntPtr memory = IntPtr.Zero;
            IntPtr _size = Marshal.AllocCoTaskMem(sizeof(int));

            _test(ref memory, ref _size);
            int size = (int)Marshal.PtrToStructure(_size, typeof(int));

            byte[] memoryArray = new byte[size];

            Marshal.Copy(memory, memoryArray, 0, size);

            Bitmap bmp;
            using (var ms = new MemoryStream(memoryArray))
            {
                bmp = new Bitmap(ms);
            }

            bmp.Save("test_recreated.png");
        }
    }
}