CivDav
CivDav

Reputation: 102

Accessing embedded resources in C++/CLI

I've been working with C# for a while, and I'm trying to write a .NET app in C++ this time. In C# I was able to access the managed resources from the code quite easily, the name of the resource file worked sort of like a class, so if I had a string called "abc" in a resource file called cba.resx, simply writing cba.abc returned with the string from the resource file. Even intellisense works with it. Same thing works with icons, etc.

Is it possible to do the same in C++, and if so, how? Or if not, what is the easiest way to access strings/icons in resource files?

Upvotes: 1

Views: 4316

Answers (1)

Tom Blodget
Tom Blodget

Reputation: 20812

In Visual Studio, C# projects have a resx file designer and a properties class generator. C++/CLI projects only get the resx file designer. Both get the build steps to embed the compiled resources in the assembly.

You can write a Properties class yourself or just access the resources in code where you need them like this:

auto resourceAssembly = Reflection::Assembly::GetExecutingAssembly();
// .Resources is the name generated by resxgen, e.g., from the input file name Resources.resx
auto resourceName = resourceAssembly->GetName()->Name + ".Resources"; 
auto resourceManager = gcnew Resources::ResourceManager(resourceName, resourceAssembly);
auto String1 = cli::safe_cast<String^>(resourceManager->GetObject("String1"));

Upvotes: 5

Related Questions