在C ++中将资源作为字节数组程序符号加载 [英] Load resource as byte array programmaticaly in C++

查看:162
本文介绍了在C ++中将资源作为字节数组程序符号加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里对于C#也有同样的问题:将资源加载为字节数组程序符号



所以我有一个资源(只是二进制文件 - 用户数据,dosen't真的很重要)。我需要得到一个指向字节数组表示这个资源的指针,该怎么办?资源位于vs2010(win32控制台项目)的资源文件中。我想我需要使用 FindResource LoadResource LockResource 函数。

解决方案

要获取资源的字节信息,第一步是获取资源的句柄 FindResource 或< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/ms648043%28v=vs.85%29.aspx =nofollow> FindResourceEx 。然后,使用
加载资源 LoadResource 。最后,使用 LockResource 获取数据的地址并访问 SizeofResource bytes from that point。以下示例说明此过程:

  HMODULE hModule = GetModuleHandle(NULL); //获取当前模块的句柄(可执行文件)
HRSRC hResource = FindResource(hModule,MAKEINTRESOURCE(RESOURCE_ID),RESOURCE_TYPE); //替换RESOURCE_ID和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);

为了简洁起见,错误处理当然被省略,您应该检查每个调用的返回值。 p>

Here the same question for C#: load resource as byte array programmaticaly

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.

解决方案

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 ++中将资源作为字节数组程序符号加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆