读取一个嵌入式文本文件资源Visual Studio c++
Reading an embedded text file resource Visual Studio C++
将文本文件添加为资源的步骤如下:1. 右键单击项目,添加新项目2. 选择文本文件,单击添加3.转到项目属性,配置属性->链接器->输入->嵌入托管资源文件4. 然后在文本框中添加文本文件items。txt
然后在我的。rc文件中,我放入以下代码:
#include "resource.h"
IDR_DATA1 TEXTFILE "Items.txt"
在resource.h文件中,我放入:
#define TEXTFILE 256
#define IDR_DATA1 255
In my form .cpp method:
std::string result;
char* data = NULL;
HINSTANCE hInst = GetModuleHandle(NULL);
HRSRC hRes = FindResource(hInst, MAKEINTRESOURCE(IDR_DATA1), MAKEINTRESOURCE(TEXTFILE));
if (NULL != hRes)
{
HGLOBAL hData = LoadResource(hInst, hRes);
if (hData)
{
DWORD dataSize = SizeofResource(hInst, hRes);
data = (char*)LockResource(hData);
}
else
{
MessageBox::Show("hData is null");
return "";
}
char* pkcSearchResult = strstr(data, "2000000");
if (pkcSearchResult != NULL)
MessageBox::Show(gcnew String(pkcSearchResult));
}
else
MessageBox::Show("hRes is null");
return result;
我一直得到hRes是null无论什么,出于某种原因FindResource没有找到Items.txt即使我添加了它作为一个资源使用上面的步骤,有人知道为什么FindResource()不工作吗?顺便说一句,它编译没有错误,上面的代码是在一个方法中,应该返回包含"2000000"的文本行(我为测试目的而更改)
看来在上述FindResource
函数中交换MAKEINTRESOURCE(IDR_DATA1)
和MAKEINTRESOURCE(TEXTFILE)
的位置是可行的。
它在以下宽字符变化中的操作方式绕过了上面描述的步骤1 - 4,并遵循@In silicon的解决方案:
- 确保文本文件是ANSI或Unicode取决于要求(UTF-8等需要额外的转换)
- 将文本文件复制到项目目录
-
如前所述,分别在项目rc和resource.h中添加以下语句:
#include "resource.h" IDR_DATA1 TEXTFILE "Items.txt"
对于$(ProjectDir)的子目录中的"Items.txt" 用反斜杠转义反斜杠,也可以使用完全限定的路径,但可能不可移植。
#define TEXTFILE 256 #define IDR_DATA1 255
并定义两个函数:
void LoadFileInResource(int name, int type, DWORD& size, const wchar_t *& data) // *& is passing the pointer by reference and not by val.
{
HMODULE handle = ::GetModuleHandleW(NULL);
HRSRC rc = ::FindResourceW(handle, MAKEINTRESOURCEW(name), MAKEINTRESOURCEW(type));
HGLOBAL rcData = ::LoadResource(handle, rc);
size = ::SizeofResource(handle, rc);
data = static_cast<const wchar_t*>(::LockResource(rcData));
//LockResource does not actually lock memory; it is just used to obtain a pointer to the memory containing the resource data.
}
wchar_t GetResource()
{
DWORD size = 0;
const wchar_t* 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.
wchar_t* buffer = new wchar_t[size + 1];
::memcpy(buffer, data, size);
buffer[size] = 0; // NULL terminator
delete[] buffer;
return *data;
}
data
应该提供上述pkcSearch查询的广泛实现。
相关文章:
- 参考资源文件VC++中的$(SolutionDir)
- 使用 Bazel 生成 QT 资源文件
- meson-build:wxWidgets 资源文件,用于 Windows 未构建
- VS:在构建/exe中包含资源文件
- VS2019:资源文件错误:中性(默认)(未知子语言:0x8)
- 连接资源文件中的字符串,并将其传递给菜单项字符串 (winapi) c++
- 无法在可视C++中检索资源文件数据
- C++ 从资源文件更改光标时出错
- 如何从资源文件(.rc 文件)加载位图?
- 在 Visual Studios 中关闭 rc 资源文件的编辑
- 应使用 CMake install 命令将可执行资源文件安装到何处?
- MSBuidel - 将资源文件添加到项目后静态库编译失败
- 生成的资源文件包含奇怪的包含文件
- 如何在没有QT项目的情况下使用Visual Studio中的QT资源文件
- 如何获取资源文件中对话框的按钮控件的大小和位置
- Qt 如何在宏中使用 .qrc 资源文件
- 可以一个#define一个文字字符串,其中包含用于资源文件的非ANSI字符的字符串
- 逐行读取用户定义的文本资源文件
- 是否可以从资源文件指定要使用的 COM 对话框
- 使用 waf 时如何组织测试资源文件