读取嵌入的文本文件资源 Visual Studio C++

use*_*764 6 c++ embedded-resource text-files visual-studio-2012

以下是我添加文本文件作为资源的步骤: 1. 右键单击​​项目,添加新项 2. 选择文本文件,单击添加 3. 转到项目属性,配置属性->链接器->输入->嵌入托管资源文件 4. 然后我在该文本框中添加了我的文本文件“items.txt

然后在我的 .rc 文件中,我输入以下代码:

#include "resource.h"
IDR_DATA1 TEXTFILE "Items.txt"
Run Code Online (Sandbox Code Playgroud)

在我的 resource.h 文件中,我输入:

#define TEXTFILE   256
#define IDR_DATA1  255
Run Code Online (Sandbox Code Playgroud)

在我的 form1.cpp 方法中:

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;
Run Code Online (Sandbox Code Playgroud)

无论如何,我一直让 hRes 为 null,出于某种原因,即使我使用上述步骤将其添加为资源,FindResource 也没有找到 Items.txt,有人知道为什么 FindResource() 不起作用吗?顺便说一句,它编译没有错误,上面的代码在一个方法中,应该返回包含“2000000”的文本行(我为了测试目的而改变)

Lau*_*arn 3

看来交换上述函数中MAKEINTRESOURCE(IDR_DATA1)和的位置是可行的。MAKEINTRESOURCE(TEXTFILE)FindResource

它在以下宽字符变体中的运行方式绕过了上述步骤 1 - 4,并遵循 @In Silico 的解决方案

  • 确保文本文件是 ANSI 或 Unicode,具体取决于要求(UTF-8等需要额外转换)
  • 将文本文件复制到项目目录中
  • 如前所述,在项目rc和resource.h中分别添加以下语句:

    #include "resource.h"
    IDR_DATA1 TEXTFILE "Items.txt"
    
    Run Code Online (Sandbox Code Playgroud)

    对于$(ProjectDir)的某些子目录中的“Items.txt” ,用反斜杠转义反斜杠,完全限定的路径也可以,但可能不可移植。

    #define TEXTFILE   256
    #define IDR_DATA1  255
    
    Run Code Online (Sandbox Code Playgroud)

并定义两个函数:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

data应该提供上述pkcSearch查询的 Widechar 实现。