問題描述
這里是 C# 的相同問題:以字節數組編程方式加載資源
Here the same question for C#: load resource as byte array programmaticaly
所以我有一個資源(只是二進制文件 - 用戶數據,并不重要).我需要獲取一個指向表示此資源的字節數組的指針,如何執行此操作?資源位于 vs2010(win32 控制臺項目)的 Resource Files 中.我想我需要使用winapi的FindResource
、LoadResource
和LockResource
函數.
So I've got a resource (just binary file - user data, dosen't really matter). I need to get a pointer to byte array representing this resource, how to do this ? Resource located in Resource Files of vs2010 (win32 console project). I think i need to use FindResource
, LoadResource
and LockResource
function of winapi.
推薦答案
要獲取資源的字節信息,第一步是使用FindResource 或 FindResourceEx.然后,使用加載資源LoadResource.最后,使用 LockResource 獲取數據地址并訪問 SizeofResource 字節.以下示例說明了該過程:
To obtain the byte information of the resource, the first step is to obtain a handle to the resource using FindResource or FindResourceEx. Then, load the resource using LoadResource. Finally, use LockResource to get the address of the data and access SizeofResource bytes from that point. The following example illustrates the process:
HMODULE hModule = GetModuleHandle(NULL); // get the handle to the current module (the executable file)
HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(RESOURCE_ID), RESOURCE_TYPE); // substitute RESOURCE_ID and RESOURCE_TYPE.
HGLOBAL hMemory = LoadResource(hModule, hResource);
DWORD dwSize = SizeofResource(hModule, hResource);
LPVOID lpAddress = LockResource(hMemory);
char *bytes = new char[dwSize];
memcpy(bytes, lpAddress, dwSize);
為簡潔起見,當然省略了錯誤處理,您應該檢查每個調用的返回值.
Error handling is of course omitted for brevity, you should check the return value of each call.
這篇關于在 C++ 中以編程方式將資源加載為字節數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!