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

相关文章

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

Spring如何使用注解@DependsOn控制Bean加载顺序

《Spring如何使用注解@DependsOn控制Bean加载顺序》:本文主要介绍Spring如何使用注解@DependsOn控制Bean加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录1.javascript 前言2. 代码实现总结1. 前言默认情况下,Spring加载Bean的顺

Redis分片集群、数据读写规则问题小结

《Redis分片集群、数据读写规则问题小结》本文介绍了Redis分片集群的原理,通过数据分片和哈希槽机制解决单机内存限制与写瓶颈问题,实现分布式存储和高并发处理,但存在通信开销大、维护复杂及对事务支持... 目录一、分片集群解android决的问题二、分片集群图解 分片集群特征如何解决的上述问题?(与哨兵模

浅析如何保证MySQL与Redis数据一致性

《浅析如何保证MySQL与Redis数据一致性》在互联网应用中,MySQL作为持久化存储引擎,Redis作为高性能缓存层,两者的组合能有效提升系统性能,下面我们来看看如何保证两者的数据一致性吧... 目录一、数据不一致性的根源1.1 典型不一致场景1.2 关键矛盾点二、一致性保障策略2.1 基础策略:更新数

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名