Reading an embedded Visual Studio C ++ text file resource

Here are the steps I took to add a text file as a resource: 1. Right click on the project, add a new item 2. Select the text file, click Add. 3. Go to project properties, configuration properties-> Linker-> Input-> Embed Managed Resource File 4. Then I added the text file "items.txt in this text box

Then in my .rc file I put the following code:

#include "resource.h"
IDR_DATA1 TEXTFILE "Items.txt"

      

In my resource.h file, I'll put:

#define TEXTFILE   256
#define IDR_DATA1  255

      

In my form1.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;

      

I keep getting hRes null no matter what, for some reason FindResource doesn't find Items.txt, although I added it as a resource using the steps above, does anyone know why FindResource () is not working? Btw it compiles without error and the above code is in a method that should return a string of text containing "2000000" (which I change for testing purposes)

+3


source to share


1 answer


It seems that the change in provisions MAKEINTRESOURCE(IDR_DATA1)

and MAKEINTRESOURCE(TEXTFILE)

in the above functions FindResource

will work.

The way it works in the following wide char is bypasses steps 1-4 as described above and follows from @In Silico:

  • Make sure the text file is ANSI or Unicode depending on the requirement ( UTF-8 , etc. requires additional conversion)
  • Copy the text file to your project directory
  • As said, add the following instructions to the rc and resource.h project respectively:

    #include "resource.h"
    IDR_DATA1 TEXTFILE "Items.txt"
    
          

    For "Items.txt" in some subdirectory of $ (ProjectDir), escaping backslashes also works the full path, but cannot be portable.

    #define TEXTFILE   256
    #define IDR_DATA1  255
    
          



And define two functions:

    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 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

should provide a large scale implementation of the above pkcSearch request.

0


source







All Articles