Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-05

fcl-res: using resource embedded in exe

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reinier Olislagers

Hi list,

I've noticed some code of mine using resources needed the windows unit (to get RT_RCDATA), Iand therefore wasn't cross-platform.

Tried rewriting using fcl-res, which seems to work ok at least on Windows:
uses
  ,resource
  //resource readers
  ,resreader, coffreader, winpeimagereader, elfreader, machoreader
  //misc
  ,elfconsts, cofftypes, machotypes;
...
    Resources:=TResources.Create;
    ResourceStream:=TMemoryStream.Create;
    try
// ====>>>>>> problem here
      Resources.LoadFromFile(Paramstr(0)); //load from exe. Isn't there
a more elegant way of doing this?
// ====>>>>>> end of problem
      for ResourceCounter:=0 to Resources.Count-1 do
      begin
        // Make sure we get the correct resource (must match .rc values):
        if (Resources[ResourceCounter]._Type.Name='RCDATA') and
          (Resources[ResourceCounter].Name.Name='SQLSCRIPT') then
        begin
          ResourceStream.CopyFrom(Resources[ResourceCounter].RawData,
            Resources[ResourceCounter].RawData.Size);
          break;
        end;
      end;
      if ResourceStream.Size=0 then
        raise Exception.Create('Cannot load SQL script as a resource
from the executable.');

In the code above, I'm loading the resource from the executable. However, Paramstr(0) is not reliable on all platforms. Is there a better way of doing this?

Thanks,
Reinier

Jonas Maebe
to Reinier Olislagers
Reinier Olislagers wrote
In the code above, I'm loading the resource from the executable. However, Paramstr(0) is not reliable on all platforms.

It's worse: on Unix platforms, even if you know the full path it's unsafe because the binary may have been unlinked since the program was started.

Is there a better way of doing this?

I don't know.

Jonas

Paul Ishenin
to Reinier Olislagers: I've noticed some code of mine using resources needed the windows unit (to get…

25.05.13, 20:25, Reinier Olislagers пишет:

In the code above, I'm loading the resource from the executable. However, Paramstr(0) is not reliable on all platforms. Is there a better way of doing this?

Yes.

Resource := FindResource(HInstance, 'SQLSCRIPT', RT_RCDATA);
if Resource  0 then
begin
   Stream := TResourceStream.Create(HInstance, 'SQLSCRIPT', RT_RCDATA);
   ...
end;

TResourceStream is available cross platform in classes.pas unit.

Best regards,
Paul Ishenin

Reinier Olislagers
to Paul Ishenin
Paul Ishenin wrote 9 lines
25.05.13, 20:25, Reinier Olislagers пишет: > In the code above, I'm loading the resource from the executable. > However, Paramstr(0) is not reliable on all platforms. > Is there a better way of doing this? Yes. Resource := FindResource(HInstance, 'SQLSCRIPT', RT_RCDATA);

Thanks Paul; IIRC Linux barfed on RT_RCDATA; can't/won't test it right now as FPC trunk is broken...

Paul Ishenin
to Reinier Olislagers

27.05.13, 17:47, Reinier Olislagers пишет:

> Resource := FindResource(HInstance, 'SQLSCRIPT', RT_RCDATA); Thanks Paul; IIRC Linux barfed on RT_RCDATA; can't/won't test it right now as FPC trunk is broken...

If you look at Lazarus source you may find that LCL uses similar code to load forms data from resources.

Best regards,
Paul Ishenin