0

I have loaded a a.dat file in resource in VC++ project in Visual studio as told in documentation.

Now in my main code, I want to load this data into a static memory buffer from resource pointer:

HRSRC myResource = ::FindResource(NULL, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
unsigned int myResourceSize = ::SizeofResource(NULL, myResource);
HGLOBAL myResourceData = ::LoadResource(NULL, myResource);
void* pMyBinaryData = ::LockResource(myResourceData);

Now I'm stucked on how to create a static memory buffer and store data from pMyBinaryData pointer.

Can anyone help to get this solved Please! Thanks in advance.

steelboy
  • 11
  • 5

1 Answers1

0

You have all the information you need: the size of the data (myResourceSize), and the contents of the resource (pMyBinaryData), so you can create a buffer and copy the contents into it:

void *buffer = malloc(myResourceSize);
memcpy(buffer, pMyBinaryData, myResourceSize);

buffer now holds a copy of the bytes making up your resource, and you can keep it for as long as you need - you can even call ::UnlockResource(myResourceData); without affecting buffer. Don't forget at some stage to do a free(buffer); though - unless you need it for the life of the program.

But if by static you mean a static, pre-allocated array, then you need to pre-allocate the largest possible size up front:

#define MAX_RESOURCE_SIZE 65536 // There is no maximum size - you'll need to pick one
static char buffer[MAX_RESOURCE_SIZE];
...
if (myResourceSize>MAX_RESOURCE_SIZE) {
    Error("Resource too big!");
} // if
memcpy(buffer, pMyBinaryData, myResourceSize);
John Burger
  • 3,662
  • 1
  • 13
  • 23
  • My **a.dat** file is a binary file. Will saving its content to a **char** _buffer_ not create any problem? –  Jun 11 '16 at 12:40
  • @Victor Good question - but `char` is the most fundamental type in C. You'd think it should be `byte` - but that's not even one of the normal types! `malloc(size)` is defined to return a buffer of `size` `char`s - or zero, if there's not enough memory. An array of `char` is what you want; just don't go using `printf()` or anything on the array! – John Burger Jun 11 '16 at 12:48
  • My **a.dat** file is around 97 MB. Can a buffer of that size be created? –  Jun 11 '16 at 12:52
  • You can definitely copy it - but perhaps you don't have to. So you've put the whole `a.dat` file into your executable as a resource? So your `.exe` is more than 97 MB in size? Then the pointer `pMyBinaryData` is all that you need: you can do whatever you need to with `pMyBinaryData` as long as the resource remains locked - don't call `::UnlockResouce()` and you can use `pMyBinaryData` forever. You may want to change its type though: `char* pMyBinaryData = (char *)::LockResource(myResourceData);` means that now you have a pointer-to-char, which may be more useful. – John Burger Jun 11 '16 at 12:57
  • thanks for your answer.Can you tell me how to use this pointer to create an input stream object. –  Jun 12 '16 at 06:53
  • You're trying to take a memory buffer and use it in an `istream`? Take a look here: http://stackoverflow.com/questions/4346240/how-can-i-read-from-memory-just-like-from-a-file-using-iostream But note that it may not work properly on your compiler – John Burger Jun 12 '16 at 07:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114448/discussion-between-john-burger-and-victor). – John Burger Jun 12 '16 at 07:28