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获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

Python实现将MySQL中所有表的数据都导出为CSV文件并压缩

《Python实现将MySQL中所有表的数据都导出为CSV文件并压缩》这篇文章主要为大家详细介绍了如何使用Python将MySQL数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到... python将mysql数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到另一个

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于