带数组的非托管导出 [英] Unmanaged Exports with Arrays
问题描述
我正在使用 RGiesecke DLLExport 库来生成可以从 Delphi 动态加载的 C# DLL.我有一个类似的方法:
I'm using RGiesecke DLLExport library to produce a C# DLL that can be dynamically loaded from Delphi. I have a method like:
[DllExport("GetVals", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static void GetVals([In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int[] valueList, int len)
{
valueList = new int[3];
int[] arrList = new int[] { 1, 2, 3 };
int idx = 0;
foreach (int s in arrList)
{
valueList[idx] = s;
idx++;
}
}
我希望能够从这个调用中返回一个数组,问题是我不会提前知道数组的大小,这只能在运行时确定.
I want to be able to return an array from this call, the problem is I won't know the size of the array in advance, this will only be determined at RunTime.
为了测试我做了以下(也在 C# 中)
To test I have done the following (also in C#)
IntPtr hLibrary = NativeWinAPI.LoadLibrary(DLLFileName);
IntPtr pointerToFunction1 = NativeWinAPI.GetProcAddress(hLibrary, "GetVals");
if (pointerToFunction1 != IntPtr.Zero)
{
GetVals getfunction = (GetVals)Marshal.GetDelegateForFunctionPointer(pointerToFunction, typeof(GetVals));
int[] valList= null;
int fCnt = 3;
getfunction(valList, fCnt);
if (valList != null)
{
}
}
出现错误试图读取或写入受保护的内存",这是可以理解的,因为我没有在调用方上分配内存.在实际使用中,我不知道要返回的数组的大小,因此无法预先分配内存.为了把最基本的东西放在那里,我试图简单地从 GetVals 返回一个未知大小的数组.
A get an error "attemping to read or write to protected memory" which is understandable as I am not allocating memory on the caller. In real use I don't know the size of the array to return so cannot pre-allocate the memory. To put things in there most basic I am trying to simply return an array of unknown size from GetVals.
推荐答案
您必须以不受 GC 影响的方式分配数组.你用 Marshal.AllocHGlobal 做到这一点:
You have to allocate the array in a way that it isn't affected by the GC. You do that with Marshal.AllocHGlobal:
[DllExport]
static void GetVals(out IntPtr unmanagedArray, out int length)
{
var valueList = new[]
{
1, 2, 3
};
length = valueList.Length;
unmanagedArray = Marshal.AllocHGlobal(valueList.Length * Marshal.SizeOf(typeof(int)));
Marshal.Copy(valueList, 0, unmanagedArray, length);
}
在 Delphi 端,您将获得指向第一个元素的指针和大小.要读取它,您可以将指针 arraySize-1 增加 1 次并将其放入列表或 Delphi 管理的数组中:
On the Delphi side, you will get a pointer to the first element, and the size. To read it you can increment the pointer arraySize-1 times and put it into a list or a Delphi-managed array:
uses
SysUtils,
Windows;
procedure GetVals(out unmanagedArray : PInteger; out arraySize : Integer);
stdcall;
external 'YourCSharpLib';
function GetValsAsArray : TArray<integer>;
var
unmanagedArray, currentLocation : PInteger;
arraySize, index : Integer;
begin
GetVals(unmanagedArray, arraySize);
try
SetLength(result, arraySize);
if arraySize = 0 then
exit;
currentLocation := unmanagedArray;
for index := 0 to arraySize - 1 do
begin
result[index] := currentLocation^;
inc(currentLocation);
end;
finally
LocalFree(Cardinal(unmanagedArray));
end;
end;
var
valuesFromCSharp : TArray<integer>;
index : Integer;
begin
valuesFromCSharp := GetValsAsArray();
for index := low(valuesFromCSharp) to high(valuesFromCSharp) do
Writeln(valuesFromCSharp[index]);
end.
这篇关于带数组的非托管导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!