if_zero_equals_one
if_zero_equals_one

Reputation: 1774

What is the equivalent of My.Resources in Visual-C++?

I need to iterate through all the resources in a project and basically output their names. I have this done in VB. But I can't figure out what the equivalent of My.Resources.ResourceManager is in VC++.

Here's the VB code.

Dim objResourceManager As Resources.ResourceManager = My.Resources.ResourceManager
Dim objResourceSet As Resources.ResourceSet = objResourceManager.GetResourceSet(CultureInfo.CurrentCulture, True, True)
Dim iterator As IDictionaryEnumerator = objResourceSet.GetEnumerator()

Private Sub go()
    Dim s As String = iterator.Key
    Debug.WriteLine(s)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If iterator.MoveNext Then
        go()
    Else
        iterator.Reset()
        If iterator.MoveNext Then
            go()
        Else
            Throw New Exception("No elements to display")
        End If
    End If
End Sub

And this is how far I am in VC++.

private:
        Resources::ResourceManager^ rmgnr;
        Resources::ResourceSet^ rSet;
    public:
        Form1(void)
        {

            rmgnr = gcnew System::Resources::ResourceManager(L"Resources ProjectCPP",Reflection::Assembly::GetExecutingAssembly());
            //This is the problem as I can't find the equivalent in c++
            rSet = rmgnr->GetResourceSet(CultureInfo::CurrentCulture,true,true);

Please help me figure this out.

Upvotes: 0

Views: 810

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283634

I think you just want:

rmgnr = gcnew System::Resources::ResourceManager(GetType());

Upvotes: 2

Icemanind
Icemanind

Reputation: 48686

You can use something like the following for unmanaged C++:

HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(resourceId), type);
HGLOBAL hRes = LoadResource(hInstance, hResInfo);
LPVOID memRes = LockResource(hRes);
DWORD sizeRes = SizeofResource(hInstance, hResInfo);

You will need to change the type and resourceId to match your resource. Not sure if its an image or icon or what kind of resource, but you would use something like:

FindResource(hInstance, MAKEINTRESOURCE(bitmapId), _T("PNG"));

For Managed C++, try something like the following:

Bitmap *MyBitmap;
String *Msg;
Reflection::Assembly *MyAssembly;
IO::Stream *ResourceStream;

MyAssembly = System::Reflection::Assembly::GetExecutingAssembly();
ResourceStream = MyAssembly->GetManifestResourceStream(ImageName);

if (ResourceStream != NULL)
{
    MyBitmap = new Bitmap(ResourceStream);
    Msg = String::Format("GetIcon: {0}, OK", ImageName);
}
else
    Msg = String::Format("GetIcon: {0}, Failed", ImageName);

// MyBitmap countains your resource

You will need to replace ImageName with the name of your resource you are trying to grab. Again, I'm assuming its an image resource you are trying to grab.

Upvotes: 0

Related Questions