anand
anand

Reputation: 11349

How to add a text file as resource in VC++ 2005?

I want to add a text file as resource in VC++ 2005. I am not able to find text as option in resource template.

Also once added how can I refer to that file while programming?

Upvotes: 8

Views: 7615

Answers (1)

humbagumba
humbagumba

Reputation: 2074

That's pretty simple: In your solution, switch to resource view, right click on your RC file, select "Add Resource", click on "Import", select "All files" then open the file you want. You're prompted to type in a custom resource type. Enter "TEXT" for example.

You can now load your custom resource like this:

HRSRC hRes = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_TEXT1), _T("TEXT"));
DWORD dwSize = SizeofResource(GetModuleHandle(NULL), hRes);
HGLOBAL hGlob = LoadResource(GetModuleHandle(NULL), hRes);
const BYTE* pData = reinterpret_cast<const BYTE*>(::LockResource(hGlob));

You don't need to unlock or free the resource, so this code can be used exactly as written without any additional calls. The resource will be freed when your program exits.

Upvotes: 11

Related Questions