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

。_饼干妹妹 提交于 2019-12-10 02:45:01

问题


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?


回答1:


You can use the FindResource API, something like

if(FindResource(hInstance, PChar(ResourceName), RT_RCDATA) <> 0)then begin
   // load the resource
end



回答2:


Use the Win32 API FindResource() function.



来源:https://stackoverflow.com/questions/9744979/how-do-i-check-if-an-embedded-resource-exists-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!