C# 非托管导出/Delphi/.NET 4/Robert Giesecke
我已经成功地使用Roberts从Delphi2007调用了.NET2/3.5程序集 但是,当我使用VS2010将C#程序集重新编译到目标.NET 4时,调用因ntdll.dll中的堆栈溢出异常而崩溃。加载mscorlib/mscoreei后(ntdll调用ntdll) 在瞄准.NET 4时,还有其他人能做到这一点吗罗伯特的文件似乎暗示这应该有效 顺便说一下,罗伯特的作品很棒,非常有用 谢谢C# 非托管导出/Delphi/.NET 4/Robert Giesecke,c#,.net,.net-4.0,C#,.net,.net 4.0,我已经成功地使用Roberts从Delphi2007调用了.NET2/3.5程序集 但是,当我使用VS2010将C#程序集重新编译到目标.NET 4时,调用因ntdll.dll中的堆栈溢出异常而崩溃。加载mscorlib/mscoreei后(ntdll调用ntdll) 在瞄准.NET 4时,还有其他人能做到这一点吗罗伯特的文件似乎暗示这应该有效 顺便说一下,罗伯特的作品很棒,非常有用 谢谢 Myles.阵列比较棘手,因为您需要更加注意阵列的分配和销毁位置。最干净的方法总是在调用者处分配,将数组传
Myles.阵列比较棘手,因为您需要更加注意阵列的分配和销毁位置。最干净的方法总是在调用者处分配,将数组传递给被调用者,让其填充数组。这种方法在您的上下文中看起来是这样的:
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;
}
可以这样称呼:
type
TSample = record
Name: WideString;
end;
PSample = ^TSample;
function func(samples: PSample; var len: Integer): Integer; stdcall;
external dllname;
var
samples: array of TSample;
i, len: Integer;
....
len := 10;
SetLength(samples, len);
if func(PSample(samples), len)=0 then
for i := 0 to len-1 do
Writeln(samples[i].Name);
更新 正如AlexS[discovered][1](请参见下面的注释),只有.net 4支持通过引用传递size参数索引。在早期版本中,需要按值传递size参数索引 我选择在此处通过引用传递它的原因是考虑到以下协议:
或许可以问问@RobertGiesecke自己?我们有一个Delphi2010应用程序,调用用.NET4编译的DLL。很好。你有什么失败的代码?似乎还记得我必须停止返回字符串作为函数结果。只返回int值,任何字符串传递都是通过out参数或类似的方式完成的。非常感谢您提供的信息-是的,.NET函数正在返回字符串。现在我知道它可以与.NET4一起使用,我将返回并在上面花费更多的时间。