Recent

Author Topic: Resource 100 not found.  (Read 1919 times)

osman.schltz

  • New Member
  • *
  • Posts: 45
Resource 100 not found.
« on: August 12, 2017, 09:44:02 pm »
Hi folks! It's me again. I found a code on Google for adding resource files to an executable. I have two projects:
1)Creates resource file
2)Reads created resource file with TResourceStream

Project 1(works fine):
Code: Pascal  [Select][+][-]
  1. unit RCDataCreatorForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.  
  11.   { TForm1 }
  12.  
  13.   TForm1 = class(TForm)
  14.     Button1: TButton;
  15.     Button2: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure Button2Click(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26. type
  27.   TResHeader = record
  28.     DataSize: DWORD;
  29.     HeaderSize: DWORD;
  30.     ResType: DWORD;
  31.     ResId: DWORD;
  32.     DataVersion: DWORD;
  33.     MemoryFlags: WORD;
  34.     LanguageId: WORD;
  35.     Version: DWORD;
  36.     Characteristics: DWORD;
  37.   end;
  38.  
  39. procedure CreateResourceFile(
  40.   DataFile, ResFile: string;  // file names
  41.   ResID: Integer              // id of resource
  42. );
  43. var
  44.   FS, RS: TFileStream;
  45.   FileHeader, ResHeader: TResHeader;
  46.   Padding: array[0..SizeOf(DWORD)-1] of Byte;
  47. begin
  48.   FS := TFileStream.Create(  // to read data file
  49.     DataFile, fmOpenRead);
  50.   RS := TFileStream.Create(  // to write res file
  51.     ResFile, fmCreate);
  52.   { Create res file header - all zeros except
  53.     for HeaderSize, ResType and ResID }
  54.   FillChar(FileHeader, SizeOf(FileHeader), #0);
  55.   FileHeader.HeaderSize := SizeOf(FileHeader);
  56.   FileHeader.ResId := $0000FFFF;
  57.   FileHeader.ResType := $0000FFFF;
  58.   { Create data header for RC_DATA file }
  59.   FillChar(ResHeader, SizeOf(ResHeader), #0);
  60.   ResHeader.HeaderSize := SizeOf(ResHeader);
  61.   // resource id - FFFF says "not a string!"
  62.   ResHeader.ResId := $0000FFFF or (ResId shl 16);
  63.   // resource type - RT_RCDATA (from Windows unit)
  64.   ResHeader.ResType := $0000FFFF
  65.     or (WORD(RT_RCDATA) shl 16);
  66.   // data file size is size of file
  67.   ResHeader.DataSize := FS.Size;
  68.   // set required memory flags
  69.   ResHeader.MemoryFlags := $0030;
  70.   { Write the headers to the resource file }
  71.   RS.WriteBuffer(FileHeader, sizeof(FileHeader));
  72.   RS.WriteBuffer(ResHeader, sizeof(ResHeader));
  73.   { Copy the file into the resource }
  74.   RS.CopyFrom(FS, FS.Size);
  75.   { Pad data out to DWORD boundary }
  76.   if FS.Size mod SizeOf(DWORD) <> 0 then
  77.     RS.WriteBuffer(Padding, SizeOf(DWORD) -
  78.       FS.Size mod SizeOf(DWORD));
  79.   { Close the files }
  80.   FS.Free;
  81.   RS.Free;
  82. end;
  83.  
  84. procedure TForm1.Button1Click(Sender: TObject);
  85. begin
  86.   // Create out resource file
  87.   CreateResourceFile('Hello.rtf', 'Hello.res', 100);
  88.   // Show a message to say we've done
  89.   ShowMessage('Resource file Hello.res has been created'#10#10
  90.     + 'Now build RCDataUser.dpr and run it');
  91. end;
  92.  
  93. procedure TForm1.Button2Click(Sender: TObject);
  94. begin
  95.   // Close the app
  96.   Close;
  97. end;
  98.  
  99. end.
  100.  

Project 2(When I run it and click button1, it gives error : 'Resource 100 not found' :
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  9.   StdCtrls, ComCtrls, RichMemo;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     RichMemo1: TRichMemo;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure Button2Click(Sender: TObject);
  21.     procedure RichMemo1Change(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. var
  37.   RS: TResourceStream;
  38. begin
  39.   // Create resource stream
  40.   RS := TResourceStream.CreateFromID(HInstance, 100, RT_RCDATA);
  41.   try
  42.     // Load the rich edit component
  43.     RichMemo1.LoadRichText(RS);
  44.   finally
  45.     // Free the stream
  46.     RS.Free;
  47.   end;
  48. end;
  49.  
  50. procedure TForm1.Button2Click(Sender: TObject);
  51. begin
  52.   Close;
  53. end;
  54.  
  55. procedure TForm1.RichMemo1Change(Sender: TObject);
  56. begin
  57.  
  58. end;
  59.  
  60. end.

Please help, thanks!
Windows 8.1
Linux Ubuntu 14.04 LTS

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Resource 100 not found.
« Reply #1 on: August 12, 2017, 11:09:32 pm »
So, where are you actually reading or including 'Hello.res' in your Project2?

You're using CreateFromID(HInstance... but nowhere do I see that you are actually addressing the resource file Hello.res.

(I don't see you using {$R Hello.res} anywhere either)

osman.schltz

  • New Member
  • *
  • Posts: 45
Re: Resource 100 not found.
« Reply #2 on: August 13, 2017, 01:04:38 pm »
Hi, rvk. True I don't added
Code: Pascal  [Select][+][-]
  1. {$R Hello.res}
. Because I found it like this. Thanks. I will add this.
Windows 8.1
Linux Ubuntu 14.04 LTS

 

TinyPortal © 2005-2018