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);  
 


猜你喜欢

转载自blog.csdn.net/thanklife/article/details/80305276
今日推荐