如何检查嵌入式资源是否存在? [英] How do I check if an embedded resource exists or not?

查看:102
本文介绍了如何检查嵌入式资源是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi中,我正在构建HTTP应用程序或Web服务器。它本质上是一个内置到单个EXE文件的整个网站。我嵌入的文件包括HTML,JS,CSS,SWF,PNG,XML等。资源名称与原始文件名相同,。替换为一个 _ 。最后,会有大约40-60个文件嵌入到EXE中。



问题是我不想编写包装每个单独文件的代码。现在,我正在为每个资源声明一个常量,并使用 TResourceStream 获取资源时使用该常量。 HTTP请求是要求任何特定的文件,并且由于我有一堆文件,我不想单独处理每个文件的方式。此外,在将来,当我添加一个要嵌入的文件时,我所要做的就是把它添加到我的资源脚本(.rc)中。所以我决定改变我的机制来自动解析所要求的文件名。例如, /Home.HTML 被解析为 HOME_HTML ,这应该是嵌入式资源的名称。我需要在加载之前检查是否存在这样的资源。



我可以尝试加载它并捕获任何异常但如果资源不存在,则会在调试中产生错误。 / code>?

解决方案

您可以使用 FindResource API,像

  if(FindResource(hInstance,PChar(ResourceName),RT_RCDATA))0然后开始
//加载资源
end


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?

解决方案

You can use the FindResource API, something like

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

这篇关于如何检查嵌入式资源是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆