locked
How to reference resources within dll? RRS feed

  • Question

  • User186656733 posted

    I know this seems really obvious, but I am having an issue here.

    I have to reference string resources in an external dll (which I cannot change).

    Here is my code that results in an error:

    Dim rm As ResourceManager

    rm = New ResourceManager("Resources", Assembly.Load("MyDll"))
    ' Get a string
    return rm.GetString("MyString")

    rm.GetString results in this error:

    "Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "Resources.resources" was correctly embedded or linked into assembly "MyDll" at compile time, or that all the satellite assemblies required are loadable and fully signed."

    Why is it looking for Resources.resources when I specified only "Resources" in New ResourceManager statement?

    Help most appreciated.

    Sunday, September 18, 2011 10:52 PM

Answers

  • User186656733 posted

    Thank you for your help.

    I did not quite understand your solution, so I pursued another path, and Tada! A solution was found.

    Examining the error message carefully, I noticed that the system (whatever it may be) was looking for an extra “.resources”.  So, on a whim, I opened Resources.Designer.vb and added “.resources” to the namespace (without quotes).

    Obviously – a bug!  But now it works.

    I appreciate all your time and help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 20, 2011 11:01 PM

All replies

  • User-1702842773 posted

    I think your assembly is not loaded properly.Try using Assembly.LoadFile(). And about your question I think every single Resource Content is ended with .resource property.

    Monday, September 19, 2011 2:30 AM
  • User186656733 posted

    Thank you for your quick reply.

    I tried your suggestion, and still encountering the same error.  I think the issue is not loading the dll, but rather, finding the resources therein.

    Is my code right? (see above).

    Help most appreciated.

    Monday, September 19, 2011 10:03 AM
  • User-1702842773 posted

    A MissingManifestResourceException is thrown when the
    attempt to retrieve a resource fails because the resource set for the neutral
    culture could not be loaded from a particular assembly. Note that even though
    the exception is thrown when you attempt to retrieve a particular resource, it
    is caused by the failure to load the resource set rather than the failure to
    find the resource. The main causes of the exception include the following:

      • The resource set is not identified by its fully qualified name. For example,
        if the baseName parameter in the call to the
        ResourceManager.ResourceManager(String, Assembly) method includes
        the root name of the resource set without a namespace, but the resource set is
        assigned a namespace when it is stored in its assembly, the call to the ResourceManager.GetString method throws this exception. You can
        determine the namespace of the resource set by examining its assembly with a
        reflection tool such as Ildasm.exe (MSIL
        Disassembler)
        .

      •  

    Originally from MSDN : http://msdn.microsoft.com/en-us/library/system.resources.missingmanifestresourceexception.aspx

    I have tested and get your exception but after I changed my baseName Parameter and run it again , the program worked

    ResourceManager rm = new ResourceManager("ClassLibrary1.Properties.Resources",
                    Assembly.LoadFile("MyLibraryPath"));
                Console.Write(rm.GetString("String1"));

    try ILDasm.exe and open your assembly, and look for your resource address base (actually the Namespace hierarchy ) and type it in baseName parameter.

    hope it works for you too.

    Monday, September 19, 2011 3:08 PM
  • User186656733 posted

    Thank you very much for the detail in your response.

    Unfortunately - still not working.

    I have used the disassembler to display the dll.  Here is (part) what I saw:

    D:\Data...\Mydll.dll    (the full path to the dll)
      M A N I F E S T

       Mydll.My
          |
          |
           Mydll.My.Resources
             |
             |
              Mydll.My.Resources.Resources
                 .class public auto ansi sealed
                 .custom instance void
                      .
                      .
                      .
                 get_MyString1 : string()
                 get_MyString2 : string()

    I assumed my code should use look like this:

    rm = New ResourceManager("Mydll.My.Resources", Assembly.LoadFile(full Path To Mydll.dll))
    String s = rm.GetString("MyString1")

    Help MOST appreciated!

    Monday, September 19, 2011 9:53 PM
  • User-1702842773 posted

     If the output of ILDasm is what you have posted , you should use

    rm = New ResourceManager("Mydll.My.Resources.Resources", Assembly.LoadFile(full Path To Mydll.dll))
    String s = rm.GetString("MyString1")
    
    Tuesday, September 20, 2011 3:24 AM
  • User186656733 posted

    Thanks again for your help.

    I have tried every possible combination I could think of, but to no avail.

    In debug, I looked at “rm”, and saw that there are 0 ResourceSets. So, if I am reading this correctly, ResourceManager did not find or load any resources.

    I am stumped!  This is a real road block!

    Help would be most appreciated.

    Tuesday, September 20, 2011 3:41 PM
  • User1622957740 posted

    Another thing you can do that might be much easier:

    .NET automatically creates resource wrapper classes for resource files - they're usually in your Properties folder. By default these auto-generated classes are internal and so not visible cross assembly. But you can use the designer to set a flag to make them public (in the Resource designer click on the Access Modifier dialog and choose: Public).

    Once you've done that you can set a reference to your assembly in the project or load the assembly via code and then reference the class. It should return the appropriate resources for you as well in the current ui-culture.

    Hope this helps,

    +++ Rick ---

    Tuesday, September 20, 2011 8:09 PM
  • User186656733 posted

    Thank you for your help.

    I did not quite understand your solution, so I pursued another path, and Tada! A solution was found.

    Examining the error message carefully, I noticed that the system (whatever it may be) was looking for an extra “.resources”.  So, on a whim, I opened Resources.Designer.vb and added “.resources” to the namespace (without quotes).

    Obviously – a bug!  But now it works.

    I appreciate all your time and help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 20, 2011 11:01 PM