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