Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2005 Feb : DLL and TStream

www.cryer.info
Managed Newsgroup Archive

DLL and TStream

Subject:DLL and TStream
Posted by:"Adrien Reboisson" (adrien-reboissonatastasedotcom@nospam.com)
Date:27 Feb 2005 07:48:48

Hi,

I'm trying to create a kind of "plugin system" with Delphi 7 : I've a TFileStream loaded in the main application and I want to "send" it to a DLL (created with Delphi, too) in order to allow it to do something with (in my first try, it was to compress the stream by using TurboPower Abbrevia's components).

When I write my code in my application, without using the DLL, it works fine (no AV, no exception raised...) :

[-------------------------]

function DeflateStream(AOriginalStream, AModifiedStream: TStream;
  AParameter: PChar): Boolean;
begin
  try
    AbZipPrc.DeflateStream(AOriginalStream, AModifiedStream);
    Result := True;
  except
     Result := False;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  AStream: TFileStream;
  AZipped: TMemoryStream;
begin
  AStream := TFileStream.Create('C:\Photo 0125.JPG', fmOpenRead);
  AZipped := TMemoryStream.Create;
  try
    DeflateStream(AStream, AZipped, '');
  finally
    AStream.Free;
    AZipped.Free;
  end;
end;

[-------------------------]

Okay, now I want to let my DLL deflate the stream. I thought that I just had to move DeflateStream() into my DLL and voila... But if I do that I get random AV raised from within the Abbrevia's DeflateStream function :

[-------------------------]

< Main application >

function ExecStreamOut(AlgoUID: PChar; AParameters: PChar; AOriginalStream, AModifiedStream: TStream): Boolean; stdcall; external 'fscompress.fsmod';

procedure TForm1.Button1Click(Sender: TObject);
var
  AStream: TFileStream;
  AZipped: TMemoryStream;
begin
  AStream := TFileStream.Create('C:\Photo 0125.JPG', fmOpenRead);
  AZipped := TMemoryStream.Create;
  try
    ExecStreamOut('deflate', '', AStream, AZipped);
  finally
    AStream.Free;
    AZipped.Free;
  end;
end;

<DLL>

  function ExecStreamOut(AlgoUID: PChar; AParameters: PChar; AOriginalStream, AModifiedStream: TStream): Boolean; stdcall;
  begin
    Result := False;
    if LowerCase(AlgoUID) = A_DEFLATE then
      Result := TFsCompressKit.DeflateStream(AOriginalStream, AModifiedStream, AParameters);
  end;

class function TFsCompressKit.DeflateStream(AOriginalStream, AModifiedStream: TStream;
  AParameter: PChar): Boolean;
begin
  try
    AbZipPrc.DeflateStream(AOriginalStream, AModifiedStream); //AV raised !!!!
    Result := True;
  except
    Result := False;
  end;
end;

[-------------------------]

The code is quite identical, but it does not work. Why ?... I suppose there is no problem to pass the object, since the DLL has been wrote with Delphi (thus, the DLL "knows" what is a TStream...) Is there that the polymorphism does not work with external objects like DLLs (I send a filestream and a memorystream, but the function in the DLL wait for TStreams ?...)

Any hints appreciated !!

Best regards,

A.R.

Replies:

www.cryer.info
Managed Newsgroup Archive