回到C ++数组C# [英] Return C++ array to C#
问题描述
我似乎无法弄清楚如何从导出的C ++ DLL数组回到我的C#程序。我从谷歌上搜索使用Marshal.Copy()到阵列复制到缓冲区但不给我,我试图返回的值,我不知道是什么的给我。<发现的唯一的事情/ p>
这就是我一直在努力:
导出功能:
的externC__declspec(dllexport)的INT *测试()
{
INT ARR [] = {1,2,3,4, 5};
返回编曲;
}
C#部分:
函数[DllImport(Dump.dll)]
公众的extern静态INT []测试();
静态无效的主要(字串[] args)
{
Console.WriteLine(测试()[0]);
Console.ReadKey();
}
我知道这个返回类型为int []可能是错误的,因为的托管/非托管的不同,我只是不知道在哪里从这里走。我似乎无法找到任何东西一个答案,但返回字符数组为字符串,而不是整数数组。
我想通了原因,我与元帅得到的值。复制不是我返回的人是因为在导出函数的'改编'阵列被删除,但我不是100%肯定,如果任何人都可以清除这件事将是巨大的。
我已经实现斯利拉姆提出的解决方案,因此,以防有人在这里想它是
在++创建与此代码一个DLLç
的externC__declspec(dllexport)的INT *测试()
{
为int *改编=新INT [5];
改编[0] = 1;
改编[1] = 2;
ARR [2] = 3;
改编[3] = 4;
改编[4] = 5;
返回编曲;
}
的externC__declspec(dllexport)的INT ReleaseMemory为(int *粒子阵列)
{
删除[]粒子阵列;
返回0;
}
该DLL将被称为InteropTestApp
然后创建在C#一个控制台应用程序
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;使用System.Runtime.InteropServices
;
命名空间DLLCall
{
类节目
{
函数[DllImport(C:\\Devs\\C ++ \\ Projects\\Interop\\InteropTestApp\\Debug\\InteropTestApp.dll)]
公共静态外部的IntPtr测试();
函数[DllImport(C:\\Devs\\C ++ \\Projects\\Interop\\InteropTestApp\\Debug\\InteropTestApp。 DLL,CallingConvention = CallingConvention.Cdecl)]
公共静态外部INT ReleaseMemory(IntPtr的PTR);
静态无效的主要(字串[] args)
{
IntPtr的PTR =测试();
INT []结果=新INT [5];
Marshal.Copy(PTR,结果,0,5);
ReleaseMemory(PTR);
Console.ReadKey();
}
}
}
结果现在包含了1, 2,3,4,5。
希望有所帮助。
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.
Here's what I've been trying:
Exported function:
extern "C" __declspec(dllexport) int* Test()
{
int arr[] = {1,2,3,4,5};
return arr;
}
C# portion:
[DllImport("Dump.dll")]
public extern static int[] test();
static void Main(string[] args)
{
Console.WriteLine(test()[0]);
Console.ReadKey();
}
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.
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.
I have implemented the solution Sriram has proposed so in case someone wants it here it is
In C++ you create a DLL with this code
extern "C" __declspec(dllexport) int* test()
{
int * arr=new int[5];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
return arr;
}
extern "C" __declspec(dllexport) int ReleaseMemory(int* pArray)
{
delete[] pArray;
return 0;
}
The DLL will be called InteropTestApp
Then you create a console application in Csharp.
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[] result = new int[5];
Marshal.Copy(ptr, result, 0, 5);
ReleaseMemory(ptr);
Console.ReadKey();
}
}
}
result now contains the 1,2,3,4,5.
Hope that helps.
这篇关于回到C ++数组C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!