Welcome, Guest
Username: Password: Remember me
CodeTyphon Linux OS Development, discussions and problems
  • Page:
  • 1

TOPIC:

Resource not found 7 years 7 months ago #10001

  • avanleyen
  • avanleyen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 11
  • Thank you received: 0
I have a problem with using resources in Typhon 32 v5.8, running under Linux.

I create an empty project/application named 'kaarten' with only one form and compile/run this.
The resulting executable file is 32.4 MB and the resource file (kaarten.res) is 182KB.

Then I open the project options and add a resource. This resource is an image file with a size of 235 KB.
After this I again compile and run the program.
The program is now again 32.4MB and the resource file (kaarten.res) is 417KB.

I want the image to be the background of the form, so I add the following code:

procedure TForm1.FormCreate(Sender: TObject);
var bm: TBitmap;
begin
bm := TBitmap.Create;
bm.LoadFromLazarusResource('FELT');
end;

... and of course a
Bitmap.Fill(background);
to make the background appear on the form.

When I now compile the program it compiles OK, but gives a runtime error:
Project kaarten raised exception class 'EResNotFound' with message:
Resource 'FELT' not found

However, according to the project - options - resources the name of the resource is correct:
images/felt.png - RCDATA - FELT (filename - type - resource)

If I compare the size of the kaarten.res file I notice that it has grown by the size of the image file that I have added. However, the program file seems to be the same.

What else steps need to be taken to make the program use the resources?

Thank you

Please Log in or Create an account to join the conversation.

Resource not found 7 years 7 months ago #10002

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4512
  • Thank you received: 1101
Sir
The resource image is Bitmap type ?
PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

Resource not found 7 years 6 months ago #10003

  • avanleyen
  • avanleyen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 11
  • Thank you received: 0
No. it is a png file

Please Log in or Create an account to join the conversation.

Resource not found 7 years 6 months ago #10004

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4512
  • Thank you received: 1101
Sir
the TBitmap can load only *.bmp files

You must use TPortableNetworkGraphic class
procedure TForm1.Button1Click(Sender: TObject);
var  ip:TPortableNetworkGraphic;
begin
  ip:=TPortableNetworkGraphic.Create;
  ip.LoadFromLazarusResource('sdfsdf');   
OR
 // ip.LoadFromResourceName(HInstance,'sdfsdf');
end;

OR

use TPicture class
procedure TForm1.Button1Click(Sender: TObject);
var  it:TPicture;
begin
  it:=TPicture.Create;
  it.LoadFromLazarusResource('sdfsdf');
OR
 //  it.LoadFromResourceName(HInstance,'sdfsdf');
end;
PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

Last edit: by Sternas Stefanos.

Resource not found 7 years 6 months ago #10005

  • avanleyen
  • avanleyen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 11
  • Thank you received: 0
I have tried both suggestions. The program compiles in both cases fine, but I still keep getting the runtime error:

Project kaarten raised exception class 'EResNotFound' with message:
Resource 'FELT' not found

I have also checked the kaarten.lpi file to see if the resource is correctly mentioned:
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="kaarten"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<Icon Value="0"/>
<Resources Count="1">
<Resource_0 FileName="images/felt.png" Type="RCDATA" ResourceName="FELT"/>
</Resources>
</General>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<RequiredPackages Count="3">
<Item1>
<PackageName Value="pl_graphics32"/>
</Item1>
<Item2>
<PackageName Value="pl_bgracontrols"/>
</Item2>
<Item3>
<PackageName Value="LCL"/>
</Item3>
</RequiredPackages>
<Units Count="2">
<Unit0>
<Filename Value="kaarten.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
<Unit1>
<Filename Value="unit1.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="kaarten"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

Please Log in or Create an account to join the conversation.

Resource not found 7 years 6 months ago #10006

  • avanleyen
  • avanleyen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 11
  • Thank you received: 0
I have spend most of last night to figure out what is going wrong.

To make things more clear I decided to concentrate on using a bitmap in the resource file and create a new, empty project with all default options set.

I added the bitmap to the resource list (project -> options -> resources) and tried to access this resource in the form oncreate event:

procedure TForm1.FormCreate(Sender: TObject);
var
bm: TBitmap;
begin
bm := TBitmap.Create;
if bm.LazarusResourceTypeValid('TEST') then begin
bm.LoadFromLazarusResource('TEST');
// bm.LoadFromResourceName(HInstance, 'TEST');
// bm.LoadFromResourceID(HInstance, 0);
end;
end;

I tried all available functions that can retrieve resources (see above). None worked, they all return the infamous 'resource not found' error.
I also added a validation (LazarusResourceTypeValid) which returns false, so I can assume the resource is really not found.

I double checked the bitmap which seems without problems.

On the Internet I read on some place that the resource must be linked with:
{$R *.res}
in the implementation section
In unit1.pas I didn't see this directive so I added it, resulting in a compile error (duplicates found). After looking in the test.lpr file I saw that it is included in this file.

I have attached the bare project.

I think it is very strange that it is not working out of the box. After all, what is the use of selecting resources in the project options when you can not use them?

File Attachment:

File Name: test.zip
File Size:564 KB
Attachments:

Please Log in or Create an account to join the conversation.

Resource not found 7 years 6 months ago #10007

  • avanleyen
  • avanleyen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 11
  • Thank you received: 0
Just a small follow up:

I have checked the resulting size of the executable with and without the resource. The difference is exactly the size of the resource (the image) that is added.

I have also downloaded a resource editor to check if the resource is actually there. The tool I used is called Resource Hacker (www.angusj.com/resourcehacker) and can be run under Wine in a Linux system.
This tool shows that the bitmap is indeed in the resource file and the name is also correct. I can also view the bitmap so I am sure it is correctly added to the .res file.

After this I decided to use the function 'EnumResourceNames' to show the resources it finds in the executable file.
I added the following function:

function ResNameCallback(hModule: LongWord; lpszType: PChar; lpszName: PChar; lParam: LongInt): LongBool; stdcall;
begin
ShowMessage(lpszName);
Result := True
end;

and a Button with the code:

procedure TForm1.Button1Click(Sender: TObject);
begin
EnumResourceNames(HInstance, RT_BITMAP, @ResNameCallback, 0);
end;

When I run the program and press the button it correctly shows that it has found the resource 'TEST'



I think the problem is not the resource file or the actually produced executable. It is related to the code that I need to use the resource.

Please Log in or Create an account to join the conversation.

Last edit: by avanleyen. Reason: adding additional info to make finding the solution easier

Resource not found 7 years 6 months ago #10008

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4512
  • Thank you received: 1101
Why don't use CodeTyphon tool ctresbuilder for *.res or *.lrs ?

Just
-open ctresbuilder from Typhon=>Tools or from CTCenter=>Tools
-make a new res file "yourres.res"
-add a file *.bmp or *.png (or Drag a file(s) to ctresbuilder)
-save the new file

Finally add to your Form

implementation
{$R yourres.res}

It's VERY simple...

PS: We make ALL *.res and *.lrs file in CodeTyphon with ctresbuilder,
we don't use other tool...:)
PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

Last edit: by Sternas Stefanos.

Resource not found 7 years 6 months ago #10009

  • avanleyen
  • avanleyen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 11
  • Thank you received: 0
Hello Sternas,

I have tried your suggestion but the result stays the same.

Using EnumResourceNames it shows that the resource 'TEST' is found, but when I call:
bm := TBitmap.Create;
bm.LoadFromLazarusResource('TEST');
I get the resource not found error

Please Log in or Create an account to join the conversation.

Resource not found 7 years 6 months ago #10010

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4512
  • Thank you received: 1101
LoadFromLazarusResource('TEST')
work only with *.lrs

Give me 5 min, to look your attach project.
PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

Resource not found 7 years 6 months ago #10011

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4512
  • Thank you received: 1101
Only 1 line
procedure TForm1.FormCreate(Sender: TObject);
begin
   Image1.Picture.Bitmap.LoadFromResourceName(HInstance,'TEST');
   Image2.Picture.PNG.LoadFromResourceName(HInstance,'TEST2');
end;

look at Screen and test the new test.zip
clear out directory before build or re-build test project

PilotLogic Architect and Core Programmer
The following user(s) said Thank You: avanleyen

Please Log in or Create an account to join the conversation.

Last edit: by Sternas Stefanos.

Resource not found 7 years 6 months ago #10012

  • avanleyen
  • avanleyen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 11
  • Thank you received: 0
Sternas, you are the best :-)

In the end it turned out that I mixed several functions - some that where meant for .res files and some for .lrs

I also didn't need to use the other resource program, just including it in the project options was enough.

Please Log in or Create an account to join the conversation.

Resource not found 7 years 6 months ago #10013

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4512
  • Thank you received: 1101
Have fun ...
I must go home now..
PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

  • Page:
  • 1