Embed Text File in a Resource in a Native Windows Application

Embed Text File in a Resource in a native Windows Application

Since you're working on a native Windows application, what you want to do is to create a user-defined resource to embed the contents of the text file into the compiled resource.

The format of a user-defined resource is documented on MSDN, as are the functions for loading it.

You embed your text file in a resource file like this:

nameID typeID filename

where nameID is some unique 16-bit unsigned integer that identifies the resource and typeID is some unique 16-bit unsigned integer greater than 255 that identifies the resource type (you may define those integers in the resource.h file). filename is the path to the file that you want to embed its binary contents into the compiled resource.

So you might have it like this:

In resource.h:

// Other defines...

#define TEXTFILE 256
#define IDR_MYTEXTFILE 101

In your resource file:

#include "resource.h"

// Other resource statements...

IDR_MYTEXTFILE TEXTFILE "mytextfile.txt"

Then you load it like this (error-checking code omitted for clarity):

#include <windows.h>
#include <cstdio>
#include "resource.h"

void LoadFileInResource(int name, int type, DWORD& size, const char*& data)
{
HMODULE handle = ::GetModuleHandle(NULL);
HRSRC rc = ::FindResource(handle, MAKEINTRESOURCE(name),
MAKEINTRESOURCE(type));
HGLOBAL rcData = ::LoadResource(handle, rc);
size = ::SizeofResource(handle, rc);
data = static_cast<const char*>(::LockResource(rcData));
}

// Usage example
int main()
{
DWORD size = 0;
const char* data = NULL;
LoadFileInResource(IDR_MYTEXTFILE, TEXTFILE, size, data);
/* Access bytes in data - here's a simple example involving text output*/
// The text stored in the resource might not be NULL terminated.
char* buffer = new char[size+1];
::memcpy(buffer, data, size);
buffer[size] = 0; // NULL terminator
::printf("Contents of text file: %s\n", buffer); // Print as ASCII text
delete[] buffer;
return 0;
}

Note that you don't actually have to free the resource since the resource resides in the binary of the executable and the system will delete them automatically when the program exits (the function FreeResource() does nothing on 32-bit and 64-bit Windows systems).

Because the data resides in the executable binary, you can't modify it via the retrieved pointer directly (that's why the LoadFileInResource() function implementation stores the pointer in a const char*). You need to use the BeginUpdateResource(), UpdateResource(), and EndUpdateResource() functions to do that.

win32/C++ Embed a Text File in a Resource

Thanks to all for your comments and responses.

I was playing a lite more with my code and also searching some info about the error.
Apparently the first 2 bytes in my DATA.txt file (ÿþ) indicates that is a UTF-16 (big endian) encoding type, opening in note++ I can see is a UCS-2 LE BOM encoding type.
Finally changing the DATA.txt file to UTF-8 encoding (without BOM) let me assign the content of this resource to a variable and play with it in my code.

How do I embed and read a text file ina WP7 app?

You can use the System.Windows.Application.GetResourceStream method:

var resource = System.Windows.Application.GetResourceStream(
new Uri("textfile.txt",UriKind.Relative));

should do the trick

embed .res file as a native win 32 resource into c# application

It is supported, Project > Properties > Application tab, "Resource file" setting.

How to inject a text resource into a prebuilt native EXE?

Windows has an API for updating resources in native executables. Check out the documentation for UpdateResource and friends.

How to embed a long list of integers as a Resource in a native c++ Windows application

Save your user defined resource in a resource (*.rc) file, or copy the resource definition into a resource file of your project. If it exists, you should be able to find in Solution Explorer under Resources folder. It it does not exist, add your newly created file to this folder. Then, if you do not have a header for resource defines (usually called Resource.h), create one and add the following line to it:

#define MyNameID    1000

Note that you can place any constant instead of 1000, as long as it is not the same as some constant used for some other resource. Finally, you can access your array in the program using FindResource Win32 function:

HRSRC hResource = FindResourceW(m_hInstance, MAKEINTRESOURCE(MyNameID), L"MyTypeID");


Related Topics



Leave a reply



Submit