Recent

Author Topic: Embedding text file into Lazarus resources error  (Read 4754 times)

Paloski

  • Newbie
  • Posts: 4
Embedding text file into Lazarus resources error
« on: August 18, 2017, 04:55:16 pm »
Hi, I have an application consisting of a TForm1, TMemo, TButton and TRadioButton .

I want to display text from a .txt file in the TMemo when TRadioButton is selected and TButton copies all text in TMemo. I can do this with LoadFromFile however, now I want to "embed" the .txt file into my program so I don't have Read/write text files exposed for people to modify.

I was able to follow the tutorial below:
http://wiki.freepascal.org/Lazarus_Resources

I successfully created an embedded Image file by using glazres to create a .lrs file however, now I want to embed a .txt file and I receive the error below:
 
Compile Project, Target: project1.exe: Exit code 1, Errors: 1, Warnings: 2
Warning: windres [option(s)] [input-file] [output-file]
Warning: windres: supported targets: pe-i386 pei-i386 elf32-i386 elf32-little elf32-big srec symbolsrec verilog tekhex binary ihex
Error: Error while compiling resources -> Compile with -vd for more details. Check for duplicates.


Here is my code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$R *.lfm}
  5. {$R stuff.rc}
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  10.   ExtCtrls, Windows;
  11. type
  12.   { TForm1 }
  13.   TForm1 = class(TForm)
  14.     Button1: TButton;
  15.     Memo1: TMemo;
  16.     RadioButton1: TRadioButton;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure RadioButton1Change(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.   memo1: String;
  28.   Data: String;
  29.   RStream: TResourceStream;
  30. implementation
  31. { TForm1 }
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. begin
  34.   Memo1.SelectAll;
  35.   Memo1.CopyToClipboard;
  36. end;
  37.  
  38. procedure TForm1.RadioButton1Change(Sender: TObject);
  39. begin
  40.   RStream := TResourceStream.Create(HInstance, 'awe', RT_RCDATA);
  41.   memo1.lines.LoadFromStream(RStream);
  42.   end;
  43.  
  44. end.    
       

For the life of me I can't figure it out... seems to be something wrong with windres making the .res file?
Any help/suggestions will be greatly appreciated thanks.

Windows 10 x64
Lazarus IDE v1.6.4                 
« Last Edit: August 18, 2017, 04:56:48 pm by Paloski »

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Embedding text file into Lazarus resources error
« Reply #1 on: August 18, 2017, 07:33:56 pm »
The easiest way to get text into the exe-file is this...  :D :D :D

Code: Pascal  [Select][+][-]
  1. UNIT Unit1;
  2.  {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Classes, SysUtils, Forms, Controls, StdCtrls;
  7.  
  8.  TYPE
  9.   TForm1 = Class(TForm)
  10.  
  11.    radBtnText: TRadioButton;
  12.    memo      : TMemo;
  13.    btn       : TButton;
  14.  
  15.    Procedure FormCreate       (Sender: TObject);
  16.    Procedure btnClick         (Sender: TObject);
  17.    Procedure radBtnTextChange (Sender: TObject);
  18.  
  19.     PRIVATE
  20.      strData: String;
  21.      //resStream: TResourceStream;
  22.   End;
  23.  
  24.  VAR
  25.   Form1: TForm1;
  26.  
  27. Implementation
  28.  {$R *.LFM}
  29.  
  30.  
  31. Procedure TForm1.FormCreate(Sender: TObject);
  32.  Begin
  33.   // copy&paste your text into a memo...
  34.   strData:= memo.Text;
  35.   //memo.Free; if you don't need the memo anymore...
  36.  
  37.   memo.Text:= 'LAZARUS forever and forever and forever ...';
  38.  End;
  39.  
  40.  
  41. Procedure TForm1.btnClick(Sender: TObject);
  42.  Begin
  43.   memo.SelectAll;
  44.   memo.CopyToClipboard;
  45.  End;
  46.  
  47.  
  48. Procedure TForm1.radBtnTextChange(Sender: TObject);
  49.  Begin
  50.   memo.Text:= strData;
  51.  End;
  52.  
  53. END.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Embedding text file into Lazarus resources error
« Reply #2 on: August 18, 2017, 07:54:44 pm »
Make sure that you don't have spaces or special characters (unicode) in your project directory path.

Paloski

  • Newbie
  • Posts: 4
Re: Embedding text file into Lazarus resources error
« Reply #3 on: August 18, 2017, 08:36:06 pm »
Thanks for the help.

I found a way to get it working by using a text file and cleaned up a bit:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$R 'stuff.RC'}
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ExtCtrls, LResources, Windows;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     RadioButton1: TRadioButton;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure RadioButton1Change(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.   RStream: TResourceStream;
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37.  
  38.  
  39. procedure TForm1.Button1Click(Sender: TObject);
  40. begin
  41.   Memo1.SelectAll;
  42.   Memo1.CopyToClipboard;
  43. end;
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. begin
  47.  memo1.clear;
  48. end;
  49.  
  50.  
  51. procedure TForm1.RadioButton1Change(Sender: TObject);
  52. begin
  53.   RStream := TResourceStream.Create(HInstance, 'awe', RT_RCDATA);
  54.   memo1.lines.LoadFromStream(RStream);
  55.   end;
  56. end.
  57.  
                               
« Last Edit: August 18, 2017, 08:37:47 pm by Paloski »

Thaddy

  • Hero Member
  • *****
  • Posts: 14391
  • Sensorship about opinions does not belong here.
Re: Embedding text file into Lazarus resources error
« Reply #4 on: August 18, 2017, 08:40:10 pm »
You mean:
Code: Pascal  [Select][+][-]
  1.   RStream := TResourceStream.Create(HInstance, 'awe', RT_RCDATA);
  2.   try
  3.      memo1.lines.LoadFromStream(RStream);
  4.   finally
  5.      RStream.free;
  6.   end;
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Embedding text file into Lazarus resources error
« Reply #5 on: August 18, 2017, 09:04:21 pm »
YOU MEAN THIS...

1. USE Project: Project Options: Resources
    Right now it's 2017 !!! rc and res files are STONE AGE FUCKSHIT !!!!  >:D >:D >:D >:D >:D >:D

2. USE this:
Code: Pascal  [Select][+][-]
  1. UNIT Unit1;
  2.  {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4. Interface
  5.  USES
  6.   Classes,  SysUtils, Forms, LCLType,
  7.   Controls, StdCtrls;
  8.  
  9.  TYPE
  10.   TForm1 = Class(TForm)
  11.  
  12.    radBtnText: TRadioButton;
  13.    memo      : TMemo;
  14.    btn       : TButton;
  15.  
  16.    Procedure FormCreate       (Sender: TObject);
  17.    Procedure btnClick         (Sender: TObject);
  18.    Procedure radBtnTextChange (Sender: TObject);
  19.  
  20.     PRIVATE
  21.      strData: String;
  22.   End;
  23.  
  24.  VAR
  25.   Form1: TForm1;
  26.  
  27. Implementation
  28.  {$R *.LFM}
  29.  
  30.  
  31. Procedure TForm1.FormCreate(Sender: TObject);
  32.   Var
  33.    resStream: TResourceStream;
  34.  Begin
  35.   // copy&paste your text into a memo...
  36.   // strData:= memo.Text;
  37.   // memo.Free; if you don't need the memo anymore...
  38.  
  39.   resStream:= TResourceStream.Create(HInstance, 'HELLO', RT_RCDATA);
  40.    Try
  41.     SetLength(strData, resStream.Size);
  42.     resStream.Read(strData[1], resStream.Size);
  43.    Finally
  44.     resStream.Free;
  45.    End;
  46.  
  47.   memo.Text:= 'LAZARUS forever and forever and forever ...';
  48.  End;
  49.  
  50.  
  51. Procedure TForm1.btnClick(Sender: TObject);
  52.  Begin
  53.   memo.SelectAll;
  54.   memo.CopyToClipboard;
  55.  End;
  56.  
  57.  
  58. Procedure TForm1.radBtnTextChange(Sender: TObject);
  59.  Begin
  60.   memo.Text:= strData;
  61.  End;
  62.  
  63. END.

3. Don't use global variables ... (only if absolutely necessary !!!).
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Embedding text file into Lazarus resources error
« Reply #6 on: August 18, 2017, 10:26:17 pm »
1. USE Project: Project Options: Resources
    Right now it's 2017 !!! rc and res files are STONE AGE ****SHIT !!!!
If that were to work reliably it would be a nice solution.
Unfortunately changes in resources defined like that are not always picked up correctly.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Embedding text file into Lazarus resources error
« Reply #7 on: August 18, 2017, 11:58:32 pm »
Quote
Unfortunately changes in resources defined like that are not always picked up correctly.
Thanks for the info... I didn't know that... (I keep that in mind...).
The example works nice, but that doesn't mean it will always work correctly...

Normally I don't put everything into the exe-file, I like the idea that the user can change for example PNG's if he really want to.. of course it depends on the program and the purpose...

Some people think if they store data inside a executable then nobody can access or change it... ..far from the truth...
I don't see a big difference in one exe-file or one zip-file or 7z-file or whatever format... but of course everybody can do it as he/she likes.

EDIT:
If there are problems after a path change for one res-file, then it can be useful to delete the lib-folder and the *.exe and *.dgb file. That solved my problem once...  :)

EDIT II:
Now I know why rc-files didn't work: I tried all this with 1.6.4 FPC 3.0.2... obviously a bug...

No problem at all with 1.8:
RC-File: MyRes.rc: HELLO RCDATA Hello.txt
Unit: {$R MyRES.RC}

This way it's easy enough... I don't like to precompile a *.res file or *.lrs-file... and this way there is nothing to change if I change some files...
That's nice...  :)
« Last Edit: August 19, 2017, 05:06:46 am by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018