Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if an embedded resource exists or not?

In Delphi, I'm building an HTTP Application, or a Web Server rather. It's essentially an entire website built into a single EXE file. The files I'm embedding include HTML, JS, CSS, SWF, PNG, XML, etc. The resource names are the same as the original filename, with the . replaced with an _. In the end, there will be somewhere around 40-60 files embedded inside the EXE.

The problem is I don't want to write code wrapping each and every individual file. Right now, I am declaring a constant for each resource, and using that constant when acquiring the resource using TResourceStream. The HTTP request is asking for any particular file, and since I'll have a bunch of files, I don't want a separate way of handling each file. Plus, in the future, when I add a new file to be embedded, all I should have to do is add it to my Resource Script (.rc). So I decided to change my mechanism to automatically resolve the filename requested to the name of the resource. For example, /Home.HTML gets resolved to HOME_HTML which is supposed to be the name of the embedded resource. I need to check if such a resource exists before loading it.

I could try to load it and catch any exception but this would produce errors in debug if the resource doesn't exist. How would I go about performing such a check without using try..except?

like image 756
Jerry Dodge Avatar asked Mar 16 '12 21:03

Jerry Dodge


1 Answers

You can use the FindResource API, something like

if(FindResource(hInstance, PChar(ResourceName), RT_RCDATA) <> 0)then begin
   // load the resource
end
like image 67
ain Avatar answered Sep 19 '22 15:09

ain