怎样在C#中调用Delphi6写的DLL

Delphi教程 2025-08-31

我在编写一个系统时遇到了一个问题,无法在c#中调用delphi6写的dll,只因为dll的参数是string类型的。然后在网上找相关的资料,还是没有结果。经过我的再三琢磨,现在已经解决,特写此文章与大家分享我的喜愉!

dellphi dll文件:

///////////////////////////////////////////////////////////////////

library mydll;

uses

sysutils,

classes;

{$r *.res}

function out_char(str1:pchar;str2:pchar):pchar;stdcall;

var

temp:pchar;

begin

getmem(temp,length(str1)+length(str2)+1);

strcopy(temp,str1);

strcat(temp,str2);

result := temp;

end;

exports

out_char;

begin

end.

//////////////////////////////////////////////////////////////

在c#中调用方式:

[dllimport(mydll.dll)] public static extern string out_char(string str1,string str2);

然后就实现了dll 传string类型数据。

呵呵~~~~~~~