本文主要是介绍VC/MFC 将 数据/资源 放入resource中 (从resource加载 数据/资源),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
很多时候,我们程序(.exe,.dll)需要配合外部资源进行操作,譬如笔者有在写得 固件更新程序(FW update tool),需要将固件程序通过tool烧录到device中去。这其中通常我们会写一个genera的tool,可以support不同的固件程序(FW),在update时只需要指定某一个固件程序即可。不过有些时候,我们希望客户不要看到我们的固件程序,希望将某个或者某些固件程序包进程序中(.exe,.dll)。这样做还可以使得我们的应用程序看起来很简洁,单个档案即可。
& lt;/p>
这就需要我们将某些资源如上述的固件程序作为资源编入到程序中,在程序中调用此资源。VC提供了具体的调用方法,包括以下几个函数:
- HMODULE GetModuleHandle( LPCTSTR lpModuleName);
- Parameters
- lpModuleName
- [in] Pointer to a null-terminated string that contains the name of the module, which must be a DLL file.
- If the file name extension is omitted, the default library extension .dll is appended.
- The file name string can include a trailing point character (.) to indicate that the module name has no extension.
- 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.
- The file extensions .dll and .cpl are treated as identical when comparing module names.
- Return Values
- A handle to the specified module indicates success.
- NULL indicates failure.
- To get extended error information, call GetLastError.
- HRSRC FindResource( HMODULE hModule, LPCWSTR lpName, LPCWSTR lpType );
- Parameters
- hModule
- Handle to the module whose executable file contains the resource. In Windows CE, this cannot be set to NULL.
- lpName
- Pointer to the name of the resource. For more information, see the Remarks section.
- lpType
- 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
- RT_ACCELERATOR Accelerator table
- RT_BITMAP Bitmap resource
- RT_CURSOR Hardware-dependent cursor resource
- RT_DIALOG Dialog box
- RT_FONT Font resource
- RT_FONTDIR Font directory resource
- RT_GROUP_CURSOR Hardware-independent cursor resource
- RT_GROUP_ICON Hardware-independent icon resource
- RT_ICON Hardware-dependent icon resource
- RT_MENU Menu resource
- RT_MESSAGETABLE Message-table entry
- RT_RCDATA Application-defined resource (raw data)
- RT_STRING String-table entry
- RT_VERSION Version resource
- Return Values
- 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.
- HGLOBAL LoadResource( HMODULE hModule, HRSRC hResInfo );
- Parameters
- hModule
- 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.
- hResInfo
- Handle to the resource to be loaded. This handle must be created by using the FindResource function.
- Return Values
- A handle to the data associated with the resource indicates success. NULL indicates failure. To get extended error information, call GetLastError.
- LPVOID LockResource(HGLOBAL hResData);
- Parameters
- hResData
- [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.
- Return Values
- 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。具体使用如下:
- // string 方式
- HRSRC hr = ::FindResource(NULL,L"IDR_HAARCASCADE",L"MYRESTYPE");
- if (NULL == hr)
- {
- int ierr = GetLastError();
- return false;
- }
- ULONG nResSize = ::SizeofResource(NULL,hr); // Data size/length
- HGLOBAL hG= ::LoadResource(NULL, hr);
- if (NULL == hG || nResSize <= 0)
- {
- //fail
- }
- LPBYTE pData = (LPBYTE)LockResource(hG); // Data Ptr
- // ID方式
- CString strItem = MAKEINTRESOURCE(IDR_HAARCASCADE);
- HRSRC hr = ::FindResource(NULL,strItem,L"MYRESTYPE");
- if (NULL == hr)
- {
- int ierr = GetLastError();
- return false;
- }
- ULONG nResSize = ::SizeofResource(NULL,hr); // Data size/length
- HGLOBAL hG= ::LoadResource(NULL, hr);
- if (NULL == hG || nResSize <= 0)
- {
- //fail
- }
- LPBYTE pData = (LPBYTE)LockResource(hG); // Data Ptr
上述方法在DLL中会出现错误,通过GetLastError会得到错误码 0x00000715 ,通过Error Lookup 可以发现是 “找不到映像文件中指定的类型”,这是因为此时default resource是load此dll的.exe中的resource,需要设置为dll中的resource方可访问。
具体为:
- HMODULE ghmodule = GetModuleHandle(L"test.dll");
- HRSRC hr = ::FindResource(ghmodule,L"IDR_DATA",L"MYRESTYPE");
- if (NULL == hr)
- {
- int ierr = GetLastError();
- return false;
- }
- ULONG nResSize = ::SizeofResource(ghmodule,hr);
- HGLOBAL hG= ::LoadResource(ghmodule, hr);
- if (NULL == hG || nResSize <= 0)
- {
- return false;
- }
- LPBYTE pData = (LPBYTE)LockResource(hG);
这篇关于VC/MFC 将 数据/资源 放入resource中 (从resource加载 数据/资源)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!