写这篇博客的原因:之前我是用delphi7开发,在调用c版的dll接口时从没有出现过问题,后来升级为delphixe3版本开发时,出现一些令人头疼的问题,我费了很大劲才解决。
Delphixe3出现如下问题:
举个例子:dll文件接口定义如下
int testDLLfun(unsigned char*a,unsigned long aLen, unsigned char* b, unsigned long * bLen);
当Delphi调用该函数时,若在delphi7中char*对应的是pchar类型(delphi7中默认的pchar就是PAnsichar类型),而从delphi2010版本以上pchar类型默认指的是PWidechar,因此在delphixe3中我们要定义成PAnsichar类型,在这里我用的是静态调用dll的方式。
function testDLLfun(a:PAnsiChar;aLen:Integer; b:PAnsiChar;bLen:pInteger):Integer;stdcall; external'dllName.dll' name 'testDLLfun';
具体实现调用delphi中的testDLLfun方法如下
function test(const aStr: AnsiString): String;
var aStr1:AnsiString;
bLen,retcode:Integer;
b:PAnsiChar;
bMemoryStream:TMemoryStream;
aBytes:TBytes;
begin
bMemoryStream:=TMemoryStream.Create;
try
bLen:=2048;
Se