






















作为不同的平台应用,经常会有非托管的dll文件需要调用.
在这里需要注意的两个地方,在这里我只说delphi编写的文件.非托管dll被C#调用首先是数据类型的问题,在delphi里面如果需要被其他程序语言调用,作为参数或者返回值的字符不能使用string类型,而是pchar.而对于recode,则对应是struct
比如,在delphi里面函数原形
function A():pchar;export; stdcall;
[DllImport(@"xxx.dll")]
public static extern string A();
type TypeA=packed record
a1:pChar;
a2:pChar;
end;

function B(A:TypeA):boolean;export; stdcall;
c#
public TypeARsaKey
{
public string a1;
public string a2;
}
[DllImport(@"xxx.dll")]
public static extern bool B(TypeA A);
第二个问题是如果我们需要返回recode,那么我们不能使用函数返回模式,而应该使用var形式
delphi:
type TypeA=packed record
a1:pChar;
a2:pChar;
end;

function B(var A:TypeA):boolean;export; stdcall;
c#
public TypeARsaKey
{
public string a1;
public string a2;
}
[DllImport(@"xxx.dll")]
public static extern bool B(ref TypeA A);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。