Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   FreePascal (https://www.delphipraxis.net/74-freepascal/)
-   -   FreePascal Text von Res (Resource Datei) in Lazarus-Projekt laden, Filestream leer [gelöst] (https://www.delphipraxis.net/174476-text-von-res-resource-datei-lazarus-projekt-laden-filestream-leer-%5Bgeloest%5D.html)

Ginko 24. Apr 2013 11:02


Text von Res (Resource Datei) in Lazarus-Projekt laden, Filestream leer [gelöst]
 
Hallo, diese Anleitung http://wiki.freepascal.org/Lazarus_Resources habe ich benutzt und eine RES-Datei für Lazarus zu erstellen. Hat auch geklappt und die RES-Datei ist genau so groß wie der ursprüngliche Text der ausgangsdatei.
Allerdings ist der Filestream immer leer wenn ich ihn eingelesen habe. (Wenn ich direkt mit dem Debugger reingehe meine ich, klar ist das er bei finally ja wieder freigeben wird.)
Es kommt kein Compilerfehler.


Hier der Code:

Delphi-Quellcode:
program project1;

{$mode objfpc}{$H+}


uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, Unit1
  { you can add units after this },
  Classes, SysUtils, FileUtil, Controls, Graphics, Dialogs, StdCtrls,
  Windows;

//{$R *.res}
{$R xId.res}

var
  S: TResourceStream;
  F: TFileStream;
    StrList:TStringList;
begin
  // create a resource stream which points to our resource
  S := TResourceStream.Create(HInstance, 'XID', RT_RCDATA);
  // Please be aware of writing an apostrophes in resource type - source will not be axtracted!!!
  try
    // create a file mydata.dat in the application directory
    F := TFileStream.Create(ExtractFilePath(ParamStr(0)) + 'xId.txt', fmCreate);
        StrList := TStringList.Create;
    try
      F.CopyFrom(S, S.Size); // copy data from the resource stream to file stream
      StrList.LoadFromStream(F);
    finally
      F.Free; // destroy the file stream
      StrList.Free;
    end;
  finally
    S.Free; // destroy the resource stream
  end;

  RequireDerivedFormResource := True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Ginko 24. Apr 2013 13:13

AW: Text von Res (Resource Datei) in Lazarus-Projekt laden, Filestream bleibt leer
 
Ok ich habs hinbekommen, allerdings etwas anders. Mit dem F.CopyFrom(S, S.Size); scheint irgendwas schief zu gehen. Aber so wie unten gehts.
Delphi-Quellcode:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,Windows;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}
{$R xId.res}

{ TForm1 }

procedure TForm1.Button2Click(Sender: TObject);
var
  rs: TResourceStream;
begin
  rs := TResourceStream.Create(hinstance, 'XID', RT_RCDATA);
  try
    Memo1.Lines.LoadFromStream(rs);
  finally
    rs.Free;
  end;
end;

end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:42 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz