7

Here are the steps I took to add a text file as a resource: 1. Right click project, add New Item 2. Choose text file, click add 3. Go to project properties, configuration properties->Linker->Input->Embed Managed Resource File 4. I then added my text file "items.txt in that textbox

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

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

In my resource.h file, I 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 is null no matter what, for some reason FindResource is not finding Items.txt even though I added it as a resource using the steps above, anyone know why FindResource() isn't working? Btw it compiles with no errors and the above code is in a method that is supposed to return the line of text that contains "2000000" (which I'm changed for testing purposes)

4
  • You might need to add the path to your text file and not just the file "Items.txt" Commented Aug 12, 2014 at 4:37
  • Just a note: You don't have to call GetModuleHandle yourself. You can simply pass NULL instead of hInst as first argument to FindResource. Commented May 4, 2015 at 11:33
  • 2017, still no answer?? Commented Sep 10, 2017 at 1:32
  • There's also UDR as well. Commented Feb 21, 2018 at 13:06

1 Answer 1

3

It appears that swapping the positions of MAKEINTRESOURCE(IDR_DATA1) and MAKEINTRESOURCE(TEXTFILE) in the above FindResource function would work.

The way it operates in the following wide char variation bypasses steps 1 - 4 as described above, and follows from @In Silico's solution:

  • Ensure textfile is either ANSI or Unicode depending on requirement (UTF-8 etc. requires extra conversion)
  • Copy the textfile into the project directory
  • As already described, add the following statements to the project rc and resource.h respectively:

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

    For "Items.txt" in some subdirectory of $(ProjectDir) escape backslash with backslash, a fully qualified path works as well, but may not be portable.

    #define TEXTFILE 256 #define IDR_DATA1 255 

And define the 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'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 should provide a widechar implementation of the above pkcSearch query.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.