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

相关文章

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置