将 C++ 数组返回到 C#

Return C++ array to C#(将 C++ 数组返回到 C#)

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

问题描述

我似乎不知道如何将一个数组从导出的 C++ DLL 返回到我的 C# 程序.我从谷歌搜索中发现的唯一一件事是使用 Marshal.Copy() 将数组复制到缓冲区中,但这并没有给我想要返回的值,我不知道它给了我什么.

I can't seem to figure out how to return an array from an exported C++ DLL to my C# program. The only thing I've found from googling was using Marshal.Copy() to copy the array into a buffer but that doesn't give me the values I'm trying to return, I don't know what it's giving me.

这是我一直在尝试的:

导出函数:

extern "C" __declspec(dllexport) int* Test() 
{
    int arr[] = {1,2,3,4,5};
    return arr;
}

C#部分:

    [DllImport("Dump.dll")]
    public extern static int[] test();

    static void Main(string[] args)
    {

        Console.WriteLine(test()[0]); 
        Console.ReadKey();


    }

我知道返回类型 int[] 可能是错误的,因为托管/非托管的差异,我只是不知道从哪里开始.除了将字符数组返回到字符串而不是整数数组之外,我似乎找不到任何答案.

I know the return type int[] is probably wrong because of the managed/unmanaged differences, I just have no idea where to go from here. I can't seem to find an answer for anything but returning character arrays to strings, not integer arrays.

我认为我使用 Marshal.Copy 获得的值不是我返回的值的原因是因为导出函数中的 'arr' 数组被删除了,但我不能 100% 确定,如果有人可以的话把这个弄清楚就好了.

I figured the reason the values I'm getting with Marshal.Copy are not the ones I'm returning is because the 'arr' array in the exported function gets deleted but I'm not 100% sure, if anyone can clear this up that would be great.

推荐答案

我已经实施了 Sriram 提出的解决方案.万一有人想要它在这里.

I have implemented the solution Sriram has proposed. In case someone wants it here it is.

在 C++ 中,您可以使用以下代码创建一个 DLL:

In C++ you create a DLL with this code:

extern "C" __declspec(dllexport) int* test() 
{
    int len = 5;
    int * arr=new int[len+1];
    arr[0]=len;
    arr[1]=1;
    arr[2]=2;
    arr[3]=3;
    arr[4]=4;
    arr[5]=5;
        return arr;
}

extern "C" __declspec(dllexport) int ReleaseMemory(int* pArray)
{
    delete[] pArray;
    return 0;
}

DLL 将被称为 InteropTestApp.

然后你在 C# 中创建一个控制台应用程序.

Then you create a console application in C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace DLLCall
{
    class Program
    {
        [DllImport("C:\Devs\C++\Projects\Interop\InteropTestApp\Debug\InteropTestApp.dll")]
        public static extern IntPtr test();

        [DllImport("C:\Devs\C++\Projects\Interop\InteropTestApp\Debug\InteropTestApp.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int ReleaseMemory(IntPtr ptr);

        static void Main(string[] args)
        {
            IntPtr ptr = test();
            int arrayLength = Marshal.ReadInt32(ptr);
            // points to arr[1], which is first value
            IntPtr start = IntPtr.Add(ptr, 4);
            int[] result = new int[arrayLength];
            Marshal.Copy(start, result, 0, arrayLength);

            ReleaseMemory(ptr);

            Console.ReadKey();
        }
    }
}

result 现在包含值 1,2,3,4,5.

希望对您有所帮助.

这篇关于将 C++ 数组返回到 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 C++ 数组返回到 C#

基础教程推荐