Recent

Author Topic: Stupid Problem using a resource from an rc file  (Read 5159 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Stupid Problem using a resource from an rc file
« on: October 26, 2016, 07:03:52 pm »
I've tried this a bunch of ways so I'm assuming I'm doing something stupid.  I'm trying to add a text file as a resource using an rc file but when i try and use the resource I get a resource not found error.

the rc file is mydata.rc and has only one line in it:

ASAMPLE  RCDATA  "samples/adminvoice.txt"

The file is found since if I change either the directory or the file name in for rc file I get an error when compiling.

Here is the code I'm using:

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

When I click the button the code gives me an error at the TResourceStream.Create line:

Project unformemu raised exception class 'EResNotFound' with message: Resource "ASAMPLE" not found


The rc file is getting compiled as I can find it in the lib/i386-win32 directory as mydata.res and even if I put that file in the main directory and change the mydata.rc to mydata.res I get the same error.

I've used this before but for a graphic file so I guess I'm missing something stupid but for the life of me I don't see what.

Ideas??

Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: Stupid Problem using a resource from an rc file
« Reply #1 on: October 26, 2016, 07:18:25 pm »
You must include the compiled resource, not the source for it.

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. {$R *.lfm}
  4. {$R mydata.res}

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Stupid Problem using a resource from an rc file
« Reply #2 on: October 26, 2016, 07:27:28 pm »
Not according to the wiki page  http://wiki.freepascal.org/Lazarus_Resources  and the fact that this worked when I used a binary file in another program I wrote.


Here is the revelant section:

Quote
Adding resources to your program
Let's review the situation when you need to store some data inside your executable and during the program run you want to extract this data from it.
First we need to tell the compiler what files to include in the resource. We do this in a .rc (resource script) file: mydata.rc file:
MYDATA         RCDATA "mydata.dat"
Here MYDATA is the resource name, RCDATA is the type of resource (look here http://msdn.microsoft.com/en-us/library/ms648009(VS.85).aspx for explanation of possible types) and "mydata.dat" is your data file.
Let's instruct the compiler to include the resource into your project:
program mydata;
 
{$R mydata.rc}
begin
end.
Behind the scenes, the FPC compiler actually instructs the resource compiler distributed with FPC to follow the .rc script to compile your data files into a binary .res resource file. The linker will then include this into the executable. Though this is transparent to the programmer, you can, if you want to, create your own .res file with e.g. the Borland resource compiler. Instead of using
{$R mydata.rc}
you'd use
{$R mydata.res}

I even tried to use the compiled res file, per my original post, and still got the same error.  I'm certain it's something stupid simple that I'm screwing up.


Fungus

  • Sr. Member
  • ****
  • Posts: 354
Re: Stupid Problem using a resource from an rc file
« Reply #3 on: October 26, 2016, 07:33:31 pm »
Make sure that "samples/adminvoice.txt" is actually existing and that the path is relative to the resourcefile or the build path.

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Stupid Problem using a resource from an rc file
« Reply #4 on: October 26, 2016, 07:52:55 pm »
I double checked and did find one thing, which didn't change anything but is strange, in the existing code i posted the  {$R mydata.rc} line is just below the uses line and when I changed the filename or location of the file it ignored it.  When I move it to  above the uses clause then it error out with a file missing message during compiling until I change the filename back to the original.  Once back though it still gives me the missing resource error. 

Is there a specific place I should be putting the {$R mydata.rc} line? 

I'm wondering if in the other program I used this in there was no form, it was in a class, so no {$R *.lfm}  in the same file, could this screw things up?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Stupid Problem using a resource from an rc file
« Reply #5 on: October 26, 2016, 07:59:20 pm »
Why not use:
-  lazarus project options
- Resources
- Add
- select file
done ?

eny

  • Hero Member
  • *****
  • Posts: 1652
Re: Stupid Problem using a resource from an rc file
« Reply #6 on: October 26, 2016, 08:05:00 pm »
Why not use:
-  lazarus project options
- Resources
- Add
- select file
done ?
Because this suffers the same problem as the actual RC file: changes are not always immediately picked up by the compiler.
You have to jump through 3 hoops, measure the outdoor and indoor temperature and check the weather to make sure the compiler picks up changes.

When making changes to the rc file, the source that contains the {$R... also must be changed to load the new contents.
Simply adding a blank line does the trick.

When it's project-specific resources I always add the rc-file to the main program sourcefile.
« Last Edit: October 26, 2016, 08:12:46 pm by eny »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Stupid Problem using a resource from an rc file
« Reply #7 on: October 26, 2016, 08:07:37 pm »
Because this suffers the same problem as the actual RC file: changes are not always immediately picked up by the compiler.
True. But, doing it manually won't change a bit about that (unless something changed ?).

BTW my project is using the actual resource file, not some dumb include file.

edit:
I've changed my reply as your changed your initial post (e.g. some of my response does not make sense anymore after your update).

I have checked and double checked. Using the method as described also updates the resource again when you compile your project.

It might be a 'change' as you suggest is required (i did not needed it but, i only did a quick test)

In my test with old lazarus (1.4 series) the thing that _is_ annoying is that the resource only gets 'permanently stored inside you project when you make another change in one of your project settings  (and directly save your project to store new settings), as the IDE does not seem to pick up on changes made in the resource configuration options.

You can check by verifying if the resource is part of your .lpi file:
Code: [Select]
      <Resources Count="1">
        <Resource_0 FileName="Some.txt" Type="RCDATA" ResourceName="SOME"/>
      </Resources>
« Last Edit: October 26, 2016, 09:06:09 pm by molly »

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Stupid Problem using a resource from an rc file
« Reply #8 on: October 26, 2016, 08:07:46 pm »
I just tried that and the options screen shows the resource as ADMINVOICE, the text file is adminvoice.txt, but when I try and use it in the TResourceStream.create line I get the same error. 

Could it be the "RS := TResourceStream.Create(HInstance,'ADMINVOICE', 'RT_RCDATA'); " line itself?  Is there any way to view the resource tree in debug mode or something so I can verify that the resource is actually in and something else is causing the error?


 

eny

  • Hero Member
  • *****
  • Posts: 1652
Re: Stupid Problem using a resource from an rc file
« Reply #9 on: October 26, 2016, 08:15:36 pm »
Could it be the "RS := TResourceStream.Create(HInstance,'ADMINVOICE', 'RT_RCDATA'); " line itself?
Yes.
Don't use 'RT_RCDATA' but use the RT_RCDATA constant in the Windows unit.
Or type in MakeIntResource(10) by yourself.
So either:
  RS := TResourceStream.Create(HInstance,'ASAMPLE', MakeIntResource(10));
or
  uses Windows;
  ...
  RS := TResourceStream.Create(HInstance,'ASAMPLE', RT_RCDATA);
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Stupid Problem using a resource from an rc file
« Reply #10 on: October 26, 2016, 08:16:24 pm »
RT_RCDATA is a constant not a string.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.   public
  21.     { public declarations }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. var
  35.   RS: TResourceStream;
  36. begin
  37.   // Load Text resource.
  38.   // create a resource stream which points to our resource
  39.   RS := TResourceStream.Create(HInstance, 'SOME', PChar(10));
  40.   try
  41.     // fill memo with contents of resourcestream
  42.     Memo1.Lines.LoadFromStream(RS);
  43.   finally
  44.     // free the resource
  45.     RS.Free;
  46.   end;
  47. end;
  48.  
  49. end.
  50.  

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Stupid Problem using a resource from an rc file
« Reply #11 on: October 26, 2016, 08:25:46 pm »
Is there a specific place I should be putting the {$R mydata.rc} line? 
No, it does not matter as long as you place the resource alongside the unit that $R's it.

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Stupid Problem using a resource from an rc file
« Reply #12 on: October 26, 2016, 08:26:11 pm »
That did it!  I knew it was something stupid.  I pulled the original example of a google search and it used something like 'text' as the type so I assumed that it needed to be a string, since when I tried the RT_RCDATA without the quotes it failed due to not having 'windows' in the uses clause.  It would have been nice if an error message would give more appropriate info.

Thanks

 

TinyPortal © 2005-2018