0

I have a MFC DLL with CLR enabled and using .Net v4.0 in VS2010 SP1. I have added a new managed resource file called ReportStrings.resx to the root of the project. I am using the code below to access the resources. No matter what I put in the ResourceManger constructor. I am getting a FileNotFoundException when rmResources->GetString(sKey) is called. The exception says "Could not find file 'Report.resources'." I searched my application for any references to Report.resources and did not find any. Any help would be gretaly appreciated.

Thanks, Josh

public ref class ResourceGetter
{
  static ResourceManager^ rmResources = gcnew ResourceManager("Report.ReportStrings", Assembly::GetExecutingAssembly());

  public:
    static String^ GetResource(String^ sKey)
    {
      String^ sReturn = nullptr;
      String^ sTheme = String::Empty;

      try
      {
        sReturn = rmResources->GetString(sKey);
      }
      catch (Exception^ ex)
      {
        ex;
      }

      return (sReturn == nullptr) ? "[" + sKey + " not found]" : sReturn;
    }
};
4
  • Run ildasm.exe on your executable, double-click the manifest and look for the .mresource. It should be something like mumble.ReportStrings.resources with the mumble part unguessable from your question. Aug 19, 2011 at 18:53
  • @Hans Passant - This is what I found in the manifest: ".mresource public Report.ReportStrings.resources". This is also the name of the compiled resource in the Debug directory.
    – Navcomp
    Aug 19, 2011 at 18:58
  • Could not find file 'Report.resources' was that a typo or does the exception actually say 'Report.ReportStrings.resource'? I would also avoid initializing rmResources like that. Best to do so in the constructor of some class so you can be sure that the assembly is correct. Aug 19, 2011 at 19:14
  • @Hans Passant - The error does say 'Report.resources'. I have tried changing the base name in the ReportManager constructor and it always returns the same error with 'Report.resources'. I have moved ReportManager constructor to an initialize function and I have verified that the correct assembly is being returned. I have also verified that the assembly contains 'Report.ReportStrings.resources' and does not contain 'Report.resources'.
    – Navcomp
    Aug 19, 2011 at 19:26

1 Answer 1

0

Okay, after creating several test projects and recreating my application project from scratch I seem to have found the issue. If I initialize the resources in the app constructor of my main file everything works correctly. So for example, my project is named Report, so I add the initailization code to the CReportApp constructor in the Report.cpp file and all is well. Thanks for your help Hans Passant. My code is below:

ResourceGetter.h

#pragma once
using namespace System;
using namespace System::Resources;

public ref class ResourceGetter
{
  static ResourceManager^ rmResources = nullptr;

  public:
    static ResourceGetter()
    {
    }

    static void Initialize()
    {
      if ( rmResources == nullptr )
      {
        rmResources = gcnew ResourceManager("Report.ReportStrings", Assembly::GetExecutingAssembly());
        ResourceProxy::Initialize(gcnew Mnc::Utilities::GetResourceDelegate(&ResourceGetter::GetResource));
      }
    }

    static String^ GetResource(String^ sKey)
    {
      String^ sReturn = nullptr;

      try
      {
        if ( rmResources != nullptr )
          sReturn = rmResources->GetString(sKey);
        else
          System::Diagnostics::Debug::WriteLine("Failed to retrieve resource (" + sKey + "): Returned null.");
      }
      catch (Exception^ ex)
      {
        ex;
      }

      return (sReturn == nullptr) ? "[" + sKey + " not found]" : sReturn;
    }
};

Report.cpp

CReportApp::CReportApp()
{
  ResourceGetter::Initialize();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.