问题描述
我有一个c ++ dll项目,其中,我通过resource.rc文件嵌入了一些原始数据。
IDR_TEMPLATE1 RCDATAareaTemplate.bin
现在我要访问areaTemplate.bin文件从dll。如何在字节数组中读取areaTemplate.bin的内容?
首先使用或,然后使用和。
使用以获取数据的大小。
代码:
HMODULE g_hModDll;
[...]
HRSRC hRscr = FindResource(g_hModDll,MAKEINTRESOURCE(IDR_TEMPLATE1),
MAKEINTRESOURCE(RT_RCDATA));
if(hRscr){
HGLOBAL hgRscr = LoadResource(g_hModDll,hRscr);
if(hgRscr){
PVOID pRscr = LockResource(hgRscr);
DWORD cbRscr = SizeofResource(g_hModDll,hRscr);
}
}
请务必阅读以下有关LoadResource的说明: / p>
注释 LoadResource的返回类型是HGLOBAL,用于向后
兼容,句柄到全局
内存块。不要将此句柄传递给GlobalLock或GlobalFree
函数。
没有unlock resource或free resource API。
b 注释 LockResource返回的指针有效,直到模块
包含资源卸载。没有必要解锁
资源,因为系统会在创建它们的
进程终止时自动删除它们。
I have a c++ dll project, in which, I have embedded some raw data through "resource.rc" file.
IDR_TEMPLATE1 RCDATA "areaTemplate.bin"
Now I want to access the data of "areaTemplate.bin" file from the dll. How can I read the contents of "areaTemplate.bin" in a byte array?
First use FindResource or FindResourceEx, then use LoadResource and LockResource.
Use SizeofResource to get the size of datas.
Code:
HMODULE g_hModDll;
[...]
HRSRC hRscr = FindResource( g_hModDll, MAKEINTRESOURCE( IDR_TEMPLATE1 ),
MAKEINTRESOURCE( RT_RCDATA ) );
if ( hRscr ) {
HGLOBAL hgRscr = LoadResource( g_hModDll, hRscr );
if ( hgRscr ) {
PVOID pRscr = LockResource( hgRscr );
DWORD cbRscr = SizeofResource( g_hModDll, hRscr );
}
}
Be sure to read the following remark about LoadResource:
Remarks The return type of LoadResource is HGLOBAL for backward compatibility, not because the function returns a handle to a global memory block. Do not pass this handle to the GlobalLock or GlobalFree function.
There is no "unlock resource" or "free resource" APIs.
Remarks The pointer returned by LockResource is valid until the module containing the resource is unloaded. It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.
这篇关于C ++:从dll访问嵌入式资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!