我写了个能压缩解压缩字符串的动态链接库,大家帮我看看输入输出参数怎么写才对呢(100分)

  • 主题发起人 主题发起人 coolsundy
  • 开始时间 开始时间
C

coolsundy

Unregistered / Unconfirmed
GUEST, unregistred user!
DLL部分代码:用了ZLib单元
function CompressStream (aSource, aTarget: TStream): Single;
var
comprStream: TCompressionStream;
begin
comprStream := TCompressionStream.Create(
clFastest, aTarget);
try
comprStream.CopyFrom(aSource, aSource.Size);
Result := comprStream.CompressionRate;
finally
comprStream.Free;
end;
end;

procedure DecompressStream (aSource, aTarget: TStream) ;
var
decompStream: TDecompressionStream;
nRead: Integer;
Buffer: array [0..1023] of Char;
begin
aSource.Position:=0;
decompStream := TDecompressionStream.Create(aSource);
try
repeat
nRead := decompStream.Read(Buffer, 1024);
aTarget.Write (Buffer, nRead);
until nRead = 0;
finally
decompStream.Free;
end;
end;
//压缩方法:
function CompressStr(FStr:String;out CompressSize:Integer):PChar;stdcall;
var
StringStrm: TStringStream;
CompressStrm: TMemoryStream;
DataStrm: TMemoryStream;
OldLength:Integer;
begin
StringStrm:=TStringStream.Create(FStr);
OldLength:=Length(FStr);
try
CompressStrm:=TMemoryStream.Create;
DataStrm:= TMemoryStream.Create;
try
CompressStream(StringStrm,CompressStrm);
CompressStrm.Position:=0;
DataStrm.Write(OldLength,SizeOf(OldLength));
DataStrm.Write(CompressStrm,CompressStrm.Size);
DataStrm.Position:=0;

StrCopy(Result,DataStrm.Memory);
CompressSize:=DataStrm.Size;
finally
CompressStrm.Free;
end;
finally
StringStrm.Free;
end;
end;
//解压缩方法:
function DecompressStr(FStr:PChar;CompressSize:Integer):String;stdcall;
var
StringStrm: TStringStream;
MemoStrm,DataStrm: TStream;
begin
StringStrm:=TStringStream.Create('');
try
DataStrm:=TMemoryStream.Create;
MemoStrm:=TMemoryStream.Create;
try
DataStrm.WriteBuffer(FStr,CompressSize);
DataStrm.Position:=SizeOf(Integer);
MemoStrm.CopyFrom(DataStrm,DataStrm.Size-SizeOf(Integer));
DecompressStream(MemoStrm,StringStrm);
Result:=StringStrm.DataString;
finally
MemoStrm.Free;
DataStrm.Free;
end;
finally
StringStrm.Free;
end;
end;
exports
CompressStr name 'CompressStr',
DecompressStr name 'DecompressStr';

程序调用部分:

function CompressStr(FStr:widestring;out CompressSize:Integer):Pchar;stdcall;external 'SZlibDll.dll' name 'CompressStr';
function DecompressStr(FStr:PChar;CompressSize:Integer):widestring;stdcall;external 'SZlibDll.dll' name 'DecompressStr';

procedure TForm1.Button1Click(Sender: TObject);
var comPChar:PChar;
FSize:Integer;
begin
memo3.Lines.Clear;
comPChar:=CompressStr(memo2.Lines.Text,FSize);
memo3.Lines.Text:=String(comPChar);

memo1.Lines.Clear;
memo1.Lines.Text:=DecompressStr(comPChar,FSize);

end;
 
都用pchar
 
谢谢。不过你试过没有?我的代码可以拿出来调试的。都用pchar还是一样的。
 
先放到普通单元里面,测试没问题后再放入DLL中
 
我在普通单元中是正确的代码的,就是拿到DLL中不知道怎么写了。
在普通单元是用memorystream的。但是在dll中用pchar,我就不知道怎么处理呢
 
函数声明都改为这样:
function CompressStr(FStr: PChar; out CompressSize: Integer): PChar; stdcall;
function DecompressStr(FStr:PChar; CompressSize: Integer): PChar; stdcall;
 
这两天我也在晕这个问题.压缩和解压缩.不过我是对文件.
 
那你也没有找到解决办法么?文件和字符串应该是一样的处理方法吧。
 
后退
顶部