Mail to Keith Dan
keith的天空
海阔凭鱼跃,天高任鸟飞
作为不同的平台应用,经常会有非托管的dll文件需要调用.
在这里需要注意的两个地方,在这里我只说delphi编写的文件.非托管dll被C#调用首先是数据类型的问题,在delphi里面如果需要被其他程序语言调用,作为参数或者返回值的字符不能使用string类型,而是pchar.而对于recode,则对应是struct
比如,在delphi里面函数原形
function A():pchar;export; stdcall;

c#调用
[DllImport(@"xxx.dll")]
public static extern string A();

delphi
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);
posted on 2007-11-30 14:12  KeithDan  阅读(700)  评论(2编辑  收藏  举报