Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding resource in a C++/CLI project

I'd like to embed some files (text files, maybe graphics) in a C++/CLI project -- preferably the same way I can do in C# project. This might be however impossible, as I found in this post: http://bytes.com/topic/net/answers/571530-loading-markup-xamlreader-load-resource-file#post2240705. This was written three years ago, so maybe now there is some way to do this (in VS2k8)?

like image 777
liori Avatar asked Dec 31 '09 17:12

liori


People also ask

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 you add an embedded 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.

How do I add a resource to Visual Studio Project?

In Visual Studio, open a SharePoint solution. In Solution Explorer, choose a SharePoint project node, and then, on the menu bar, choose Project > Add New Item. In the Add New Item dialog box, choose the Global Resources File template, and then choose the Add button.

What is 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. Advantages.


2 Answers

Under C++/Cli project go to “Properties…”, then look under “Linker”, and then “Input”, you’ll see the list of embedded files under “Embed Managed Resource File”.

like image 76
Hamish Grubijan Avatar answered Sep 22 '22 20:09

Hamish Grubijan


This is an embellishment of Tarydon's comment, showing how to save the embedded resource to a file:

using namespace System::IO;
...
String^ tmpFilename = System::IO::Path::GetTempFileName();
try
{
   Stream^ readStream = Assembly::GetExecutingAssembly()->GetManifestResourceStream("embedded_file_name.xyz");
   if(readStream != nullptr)
   {
       FileStream^ writeStream = gcnew FileStream(tmpFilename, FileMode::Create);
       readStream->CopyTo(writeStream);
       readStream->Close();
       writeStream->Close(); // Required to flush the buffer & have non-zero filesize
   }
}
catch (...)
{
    // Do something?
}
like image 21
dlchambers Avatar answered Sep 22 '22 20:09

dlchambers