我正在使用罗伯特·吉塞克非托管导出 1.2.6 in VS2010我的目标是传递一个结构数组时间:2019-03-17 标签:c#(.NET 3.5) to 德尔福(D7)。
我必须承认,我对delphi不太熟悉。
我已经读过这个帖子,但建议的答案对我不起作用:
打电话时func
在delphi中,CPU调试窗口打开,如果我继续,应用程序毫无例外地退出,并且没有期望的结果。
这是我尝试过的代码:
C# 平台 x86
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using RGiesecke.DllExport;
namespace ArrayTest
{
public class Class1
{
public struct Sample
{
[MarshalAs(UnmanagedType.BStr)]
public string Name;
}
[DllExport]
public static int func(
[Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
Sample[] samples,
ref int len
)
{
// len holds the length of the array on input
// len is assigned the number of items that have been assigned values
// use the return value to indicate success or failure
for (int i = 0; i < len; i++)
samples[i].Name = "foo: " + i.ToString();
return 0;
}
}
}
Delphi7
program DelphiApp;
{$APPTYPE CONSOLE}
uses
SysUtils,
ActiveX;
type
TSample = record
Name: WideString;
end;
PSample = ^TSample;
function func(samples: PSample; var len: Integer): Integer; stdcall;
external 'ArrayTest.dll';
procedure Test2;
var
samples: array of TSample;
i, len: Integer;
begin
len := 10;
SetLength(samples, len);
if func(PSample(samples), len)=0 then
for i := 0 to len-1 do
Writeln(samples[i].Name);
end;
begin
Test2();
end.
如前所述,调试器打开 CPU 窗口,如果我继续,应用程序将退出,没有异常或错误消息。
如果我在没有调试器的情况下运行它,Windows 会告诉我该应用程序不再工作,并且该应用程序将关闭。
我缺少什么?
Update
修改后的代码:
[DllExport]
public static int func(
[Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
Sample[] samples,
ref int len
)
{
Console.WriteLine("return 0");
return 0;
}
procedure Test2;
var
samples: array of TSample;
i, len: Integer;
begin
len := 10;
SetLength(samples, len);
if func(PSample(samples), len)=0 then
for i := 0 to len-1 do
Writeln('D7: ', i);
end;
即使我不访问任一侧的数组,行为仍然是相同的。
控制台输出:return 0