Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded resource in C++

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.

like image 319
Uday Avatar asked Jul 02 '09 13:07

Uday


People also ask

What is an embedded resource?

Embedded resource has no predefined structure: it is just a named blob of bytes. So called “. resx” file is a special kind of embedded resource. It is a string-to-object dictionary that maps a name to an object. The object may be a string, an image, an icon, or a blob of bytes.

What is the use of embedded resource C#?

Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System. Reflection namespace. Any file within the project can be made into an embedded file.

How do I embed a resource file in C++?

Create a resource script, for instance resource. rc, and compile the resource to object code with the MSVC resource compiler. rc.exe or GCC/Mingw resource compiler, windres.exe. Then build the executable by linking the application object code with the compiled resource.

How do I add embedded resources to Visual Studio?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.


2 Answers

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

like image 98
Ismael Avatar answered Sep 22 '22 23:09

Ismael


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.

like image 36
sharptooth Avatar answered Sep 22 '22 23:09

sharptooth