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.
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.
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With