7

How do I create an embedded resource and then access it from C++?

Any example on how to read the resource would be great.

I am using Visual Studio 2005.

Thanks in advance.

Edit: I want to put one xsd file which is required while validating schema of the recieved xml file.

Ismael
  • 2,995
  • 29
  • 45
Uday
  • 819
  • 3
  • 12
  • 25

2 Answers2

10

I'm doing @Sharptooth explained before and use the following code to get the resource

HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(resourceId), type);
HGLOBAL hRes = LoadResource(hInstance, hResInfo);
LPVOID memRes = LockResource(hRes);
DWORD sizeRes = SizeofResource(hInstance, hResInfo);

Here you have to change resourceId and type.

For example for a .png file I use FindResource(hInstance, MAKEINTRESOURCE(bitmapId), _T("PNG")); (the "PNG" string is the type you used when adding a custom resource).

Ismael
  • 2,995
  • 29
  • 45
  • how do you get the handle of the dll in runtime? – Gilad Jan 20 '16 at 10:56
  • Can you check if these question helps "How can I get HINSTANCE from a DLL?" http://stackoverflow.com/questions/2126657/how-can-i-get-hinstance-from-a-dll#2126689 – Ismael Jan 22 '16 at 19:06
4

Add a resource (.rc) file to the project, put the resource description there. When building the project the resource compiler will compile the resource file and the linker will link the compiled resource file into the resulting executable module.

In runtime call FindResource(), then LoadResource() WinAPI functions.

sharptooth
  • 167,383
  • 100
  • 513
  • 979