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)?
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.
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.
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.
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”.
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?
}
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