VC/MFC 将 数据/资源 放入resource中 (从resource加载 数据/资源)

2024-03-27 11:38

本文主要是介绍VC/MFC 将 数据/资源 放入resource中 (从resource加载 数据/资源),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

很多时候,我们程序(.exe,.dll)需要配合外部资源进行操作,譬如笔者有在写得 固件更新程序(FW update tool),需要将固件程序通过tool烧录到device中去。这其中通常我们会写一个genera的tool,可以support不同的固件程序(FW),在update时只需要指定某一个固件程序即可。不过有些时候,我们希望客户不要看到我们的固件程序,希望将某个或者某些固件程序包进程序中(.exe,.dll)。这样做还可以使得我们的应用程序看起来很简洁,单个档案即可。

 & lt;/p>

    这就需要我们将某些资源如上述的固件程序作为资源编入到程序中,在程序中调用此资源。VC提供了具体的调用方法,包括以下几个函数:

[c-sharp]  view plain copy
  1. HMODULE GetModuleHandle( LPCTSTR lpModuleName);  
  2. Parameters  
  3. lpModuleName   
  4. [in] Pointer to a null-terminated string that contains the name of the module, which must be a DLL file.   
  5. If the file name extension is omitted, the default library extension .dll is appended.   
  6. The file name string can include a trailing point character (.) to indicate that the module name has no extension.   
  7. If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process. All paths are ignored; only the file name and extension are used.   
  8. The file extensions .dll and .cpl are treated as identical when comparing module names.   
  9. Return Values  
  10. A handle to the specified module indicates success.   
  11. NULL indicates failure.   
  12. To get extended error information, call GetLastError.   
 

[c-sharp]  view plain copy
  1. HRSRC FindResource( HMODULE hModule, LPCWSTR lpName, LPCWSTR lpType );   
  2. Parameters  
  3. hModule   
  4. Handle to the module whose executable file contains the resource. In Windows CE, this cannot be set to NULL.   
  5. lpName   
  6. Pointer to the name of the resource. For more information, see the Remarks section.   
  7. lpType   
  8. Pointer to the resource type. For more information, see the Remarks section. For standard resource types, this parameter can be one of the following values. Value Description   
  9. RT_ACCELERATOR Accelerator table   
  10. RT_BITMAP Bitmap resource   
  11. RT_CURSOR Hardware-dependent cursor resource   
  12. RT_DIALOG Dialog box   
  13. RT_FONT Font resource   
  14. RT_FONTDIR Font directory resource   
  15. RT_GROUP_CURSOR Hardware-independent cursor resource   
  16. RT_GROUP_ICON Hardware-independent icon resource   
  17. RT_ICON Hardware-dependent icon resource   
  18. RT_MENU Menu resource   
  19. RT_MESSAGETABLE Message-table entry   
  20. RT_RCDATA Application-defined resource (raw data)   
  21. RT_STRING String-table entry   
  22. RT_VERSION Version resource   
  23. Return Values  
  24. A handle to the specified resource's info block indicates success. To obtain a handle to the resource, pass this handle to the LoadResource function. NULL indicates failure. To get extended error information, call GetLastError.   
 

[c-sharp]  view plain copy
  1. HGLOBAL LoadResource( HMODULE hModule, HRSRC hResInfo );  
  2. Parameters  
  3. hModule   
  4. Handle to the module whose executable file contains the resource. If hModule is NULL, the system loads the resource from the module that was used to create the current process. In Windows CE 1.0 and 1.01, setting this parameter to NULL is not supported.   
  5. hResInfo   
  6. Handle to the resource to be loaded. This handle must be created by using the FindResource function.   
  7. Return Values  
  8. A handle to the data associated with the resource indicates success. NULL indicates failure. To get extended error information, call GetLastError.   
 

[c-sharp]  view plain copy
  1. LPVOID LockResource(HGLOBAL hResData);  
  2. Parameters  
  3. hResData   
  4. [in] Handle to the resource to be locked. The LoadResource function returns this handle. This parameter is listed as an HGLOBAL variable only for backwards compatibility. Do not pass any value as a parameter other than a successful return value from the LoadResource function.   
  5. Return Values  
  6. If the loaded resource is locked, the return value is a pointer to the first byte of the resource; otherwise, it is NULL.   
 

 

在.exe中可以这样添加和访问:

1,添加数据/资源:

在resource(资源)中 通过 “Add resource -> Import...” 选择需要添加的 数据/资源,其中的 resource type 可以自己命名,

需要注意的是需要利用字符串命名,譬如可以为 "MYRESTYPE",资源ID可以为字符串,譬如为"IDR_DATA",也可以使ID譬如为IDR_DATA,这两种方式在使用Findresource函数是有所区别。

 

2,访问数据/资源:

在.exe中当前load的resource即为.exe中的resource,因此在使用FindResource,LoadResource时,参数hModule可以为NULL。具体使用如下:

 

[c-sharp]  view plain copy
  1. // string 方式  
  2. HRSRC hr = ::FindResource(NULL,L"IDR_HAARCASCADE",L"MYRESTYPE");  
  3. if (NULL == hr)  
  4. {  
  5.     int ierr = GetLastError();  
  6.     return false;  
  7. }  
  8. ULONG nResSize = ::SizeofResource(NULL,hr);  // Data size/length  
  9. HGLOBAL hG= ::LoadResource(NULL, hr);  
  10. if (NULL == hG || nResSize <= 0)  
  11. {  
  12.     //fail  
  13. }  
  14. LPBYTE pData = (LPBYTE)LockResource(hG);    // Data Ptr  
  15. // ID方式  
  16. CString strItem = MAKEINTRESOURCE(IDR_HAARCASCADE);  
  17. HRSRC hr = ::FindResource(NULL,strItem,L"MYRESTYPE");  
  18. if (NULL == hr)  
  19. {  
  20.     int ierr = GetLastError();  
  21.     return false;  
  22. }  
  23. ULONG nResSize = ::SizeofResource(NULL,hr);  // Data size/length  
  24. HGLOBAL hG= ::LoadResource(NULL, hr);  
  25. if (NULL == hG || nResSize <= 0)  
  26. {  
  27.     //fail  
  28. }  
  29. LPBYTE pData = (LPBYTE)LockResource(hG);    // Data Ptr  
 

 

上述方法在DLL中会出现错误,通过GetLastError会得到错误码 0x00000715 ,通过Error Lookup 可以发现是 “找不到映像文件中指定的类型”,这是因为此时default resource是load此dll的.exe中的resource,需要设置为dll中的resource方可访问。

具体为:

[c-sharp]  view plain copy
  1. HMODULE ghmodule = GetModuleHandle(L"test.dll");  
  2. HRSRC hr = ::FindResource(ghmodule,L"IDR_DATA",L"MYRESTYPE");  
  3. if (NULL == hr)  
  4. {  
  5.     int ierr = GetLastError();  
  6.     return false;  
  7. }  
  8. ULONG nResSize = ::SizeofResource(ghmodule,hr);  
  9. HGLOBAL hG= ::LoadResource(ghmodule, hr);  
  10. if (NULL == hG || nResSize <= 0)  
  11. {  
  12.     return false;  
  13. }  
  14. LPBYTE pData = (LPBYTE)LockResource(hG);  
 


这篇关于VC/MFC 将 数据/资源 放入resource中 (从resource加载 数据/资源)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/851981

相关文章

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类