問題描述
我有一個 C++ Windows 程序.我有一個包含一些數據的文本文件.目前,文本文件是一個單獨的文件,它在運行時加載并解析.如何將其作為資源嵌入到二進制文件中?
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?
推薦答案
由于您正在開發本機 Windows 應用程序,因此您想要做的是創建一個用戶定義的資源,將文本文件的內容嵌入到編譯后的資源.
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.
用戶定義資源的格式是記錄在MSDN,加載它的函數.
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
其中 nameID
是標識資源的唯一 16 位無符號整數,typeID
是標識資源類型的某個大于 255 的唯一 16 位無符號整數(您可以在 resource.h
文件中定義這些整數).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.
所以你可能是這樣的:
在resource.h
中:
// Other defines...
#define TEXTFILE 256
#define IDR_MYTEXTFILE 101
在您的資源文件中:
#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
", buffer); // Print as ASCII text
delete[] buffer;
return 0;
}
請注意,您實際上不必釋放資源,因為資源駐留在可執行文件的二進制文件中,系統會在程序退出時自動刪除它們(函數 FreeResource()
在 32 位和 64 位 Windows 系統上沒有任何內容).
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).
因為數據駐留在可執行二進制文件中,您不能直接通過檢索到的指針修改它(這就是為什么 LoadFileInResource()
函數實現將指針存儲在 const char* 中的原因)
).您需要使用 BeginUpdateResource()
、UpdateResource()
和 EndUpdateResource()
函數來做到這一點.
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.
這篇關于在本機 Windows 應用程序的資源中嵌入文本文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!