如何用delphi调用C编写的 (*.DLL) (100分)

X

xln18

Unregistered / Unconfirmed
GUEST, unregistred user!
我delphi用的比较顺手,可是现在有个DLL使用C编写的,
逼得我非得学习C,而且我用的VC摸不着头绪。
各位大侠有没有两全其美的办法。
function cmpp_connect_to_ismg(host:String;port:integer;conn:conn_desc ):
string;
external 'api.dll' name 'cmpp_connect_to_ismg';
conn:conn_desc 是C中是这样定义的:
struct conn_desc {
char id;
dpl_unit1 type;
}
typedef unsigned char dpl_unit1;
在delphi中要使用这个参数该怎么办?

 
读取VC的DLL字符串最好用PCHAR
这样:
VC DLL代码:
(在某个CPP里面)
............
typedef unsigned char dpl_unit1;
struct conn_desc {
char id;
dpl_unit1 type;
}
extern "C" _declspec(dllexport) char* cmpp_connect_to_ismg(char host,int port,conn_desc conn)
{
.............
// 你的代码
.............
}

在DELPHI调用:
............
............
conn_desc = Record
id:char;
tp:UCHAR;
end;

type
cmpp_connect_to_ismg = function (host:pChar;port:integer;conn:conn_desc ):pChar;
............
............
implementation
{$R *.dfm}
............
procedure TForm1.Button1Click(Sender: TObject);
var
hd:thandle;
con:conn_desc;
pf:cmpp_connect_to_ismg;
begin
hd := LoadLibrary('dllex01.dll');
@pf := GetProcAddress(hd,'cmpp_connect_to_ismg');
pf('kk',1000,con);
end;
 
顶部