C++ 在本机 Windows 应用程序的资源中嵌入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2933295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embed Text File in a Resource in a native Windows Application
提问by Oliver Zheng
I have a C++ Windows program. I have a text file that has some data. Currently, the text file is a separate file, and it is loaded at runtime and parsed. How is it possible to embed this into the binary as a resource?
我有一个 C++ Windows 程序。我有一个包含一些数据的文本文件。目前,文本文件是一个单独的文件,它在运行时加载并解析。如何将其作为资源嵌入到二进制文件中?
回答by In silico
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.
由于您正在处理本机 Windows 应用程序,因此您想要做的是创建一个用户定义的资源,以将文本文件的内容嵌入到已编译的资源中。
The format of a user-defined resource is documented on MSDN, as are the functions for loading it.
用户定义的资源的格式被记录在MSDN,因为是用于装载它的功能。
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.
其中nameID
是一些唯一的 16 位无符号整数,用于标识资源,typeID
是一些大于 255 的唯一 16 位无符号整数,用于标识资源类型(您可以在resource.h
文件中定义这些整数)。filename
是您要将其二进制内容嵌入到已编译资源中的文件的路径。
So you might have it like this:
所以你可能是这样的:
In resource.h
:
在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).
请注意,您实际上不必释放资源,因为资源驻留在可执行文件的二进制文件中,系统会在程序退出时自动删除它们(该函数FreeResource()
在 32 位和 64 位 Windows 系统上不执行任何操作)。
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.
因为数据驻留在可执行二进制文件中,您不能直接通过检索到的指针修改它(这就是LoadFileInResource()
函数实现将指针存储在 a 中的原因const char*
)。您需要使用的BeginUpdateResource()
,UpdateResource()
和EndUpdateResource()
功能做到这一点。
回答by Stephen
You can use xxd
(from a linux machine or probably cygwin) to generate the data for a .h/.cc file.
您可以使用xxd
(从 linux 机器或可能是 cygwin)为 .h/.cc 文件生成数据。
There are lots of stack overflow questions that expand on this in detail:
有很多堆栈溢出问题对此进行了详细扩展: