Read resources from a DLL file - c#

I've two Visual Basic 2008 projects - one is a class library project and another is a Windows Forms project. In the class library project, I've defined some strings in the project resources (project properties > Resources tab).
I build that class library project and get the DLL file from the debug folder and added up as a reference in my Windows Forms project.
How do I read those strings from the referenced DLL file?

While you can dynamically load the DLL as ho suggests, it's fine to use a reference as you have done. In fact I would recommend using a reference unless you had a particular requirement to dynamically load the resource assembly.
As to accessing the resources there are a few things you need to do.
In the resource assembly you will need to ensure the resources are public. By default resources are set to internal which means you will not see the resources in the winforms app. Double click on Properties\Resources.resx to open the resources view. In the top toolbar you will see a label "Access Modifier" next to a combo box drop down. Change the selection to public.
You will need to reference the assembly from the forms app. You have stated you have already done this. Just a note that a better way to do this is to create a solution that contains both projects. Then in the forms app choose add reference. Click on the Projects tab up the top. Double click on the resource DLL project name. This works better than referencing the debug DLL directly since it means if you change between a release build and debug build in your forms app, it will automatically build a matching release/debug version of your resource assembly.
Once you have added the reference you can simply reference the type out of the resources DLL, e.g.
ResourceDLLNamespace.Properties.Resource.ResourceName
Just a note, you need to be aware of type name clashes if you are using the same namespace for your forms app and resource DLL. In this situation both your forms app will have access to it's own Properties.Resources class as well as that of the resource DLL. You can do two things to avoid this:
Use a different namespace between the two assemblies
In the resource assembly don't include a default Properties\Resources.resx resource dictionary. Delete it and manually add a new resource, i.e. Add New Item and select "Resources File". You should find that you will not be able to add the new resource dictionary to the Properties folder - add it to the root or some other folder as you require. This will automatically give it a different type name by virtue of being in a different folder. You still may want to avoid using the resource file name of "Resources" however, as if you have all the relevant namespaces in scope via using statements you will get the same issue that the compiler won't know which version of Resources to use.
-Donovan

I think you just use System.Reflection.Assembly.Load to load the other assembly then use the constructor of System.Resources.ResourceManager that takes an assembly.
Note, I don't think it needs to a reference for this to work.

Related

Using lib in dll for c#

I have some problems with dll. When I make dll without using of lib files (which I need), everything is fine. But when I'm trying to use some functions in dll that uses functions in lib then some exception appears:
System.DllNotFoundException, cant load a dll module (exception from HRESULT:0x8007007E).
dll file is in correct place.
P.S. using Visual Studio 2010.
What could be wrong?
More detail in the question will get you better answers. But with current information, the most likely cause is that the lib file you are referencing or one of its dependencies is unavailable. This could be because it is not in your GAC or your runtime location, a file format conflict, or a number of other things.
I'm assuming that everything compiles without errors, of course. Again, please add detail if this is not what you need.
if you are making a new class you should reference the default dlls.
in example when you make a new class library and want to use a messagebox in your code ,
you should first reference that required dll in your program(i dont mean your dll,i mean dot net default dll like system.windows.forms) and then add using something; in the top of your class.
example : we want use messagebox in a class library then :
1. first from solution explorer right click the project > add reference ,now reference manager opens now from left tabs click assemblies then framework and then find and select system.windows.forms
2. now its time to use it in our program first add this line in very top of your class file
using System.Windows.Forms; //add this line in top of your class
after that we can use messagebox without any compiler errors.
keep in mind any other dll files should be referenced this way but in windows form applications default lib files are referenced by default

Referencing a Windows Phone Runtime Component DLL/WinMD from another project

I need to implement a wrapper to expose some native C++ code to C#, and have followed this tutorial:
http://www.silverlightshow.net/items/Windows-Phone-8-Native-Code-Support.aspx
So far in my C# test project, I don't have problems instantiating a class written in C++/CX from the Runtime Component project and using methods from that class, so long as I reference the entire project (.sln).
Visual Studio doesn't allow me to reference the Runtime Component DLL alone, but does allow me to reference the .winmd file in the project. C# then recognizes the namespace correctly, however at runtime I get a TypeLoadException when trying to create the same object.
This doesn't appear to be a namespace problem (as mentioned here: Changing namespace name of C++ component in Windows Phone causes exception), since everything is alright so long as I create a project reference (or does referencing a project vs a .winmd affect the namespace somehow?).
Is it possible to bundle the Runtime Component in some form that an end user can reference it without needing to provide the entire project?
You need to add following the to WMAppManifest.xml
<ActivatableClasses>
<InProcessServer>
<Path>YourComponent.dll</Path>
<ActivatableClass ThreadingModel="both" ActivatableClassId="YourComponentNamespace.YourComponent"/>
</InProcessServer>
</ActivatableClasses>
With YourComponent being the name of your WinMD.
I think what you are seeing is a manifestation of the problem described here.
In short, when creating a WinRT component using C++, just referencing the output DLL or the output winmd is not sufficient. You need both.
I had this same problem, and (eventually) figured out that the .dll and .winmd file needed to have the same name (which was the same as the namespace they defined) and be in the same directory.
For example, if your classes are in the X::Y namespace, the files must be X.Y.dll and X.Y.winmd.
Then all I needed to do was add a reference to the .winmd file in my project (by right-clicking on the References folder for that project in the Solution Explorer, choosing "Add Reference...", then choosing "Browse" from the dialog that comes up). I didn't need to add anything to the manifest file.

ASP.NET, reference a local resource files from a class library

I'm having a hard time wrapping my mind around how the resources work in an ASP.NET project. In my solution I have 2 projects:
A Website project that contains an App_GlobalResources folder where most of my resources reside (build action: "content").
A utility project, compiled as class library (that the website references), where I need another set of resources (called "ExceptionMessages" hereafter).
Nota: each project uses its own resources, there is no cross linking involved here.
I added a new resource directly in my second project (both as "content" or "embeded resource") and tried to access it via ExceptionMessages.Tag. Intellisense is happy about it and correctly suggests the Tag but I get the following error at runtime:
"Could not find any resources appropriate for the specified culture or
the neutral culture. Make sure
"Resources.ExceptionMessages.resources" was correctly embedded or
linked into assembly "App_GlobalResources.lqgd5zqy" at compile time,
or that all the satellite assemblies required are loadable and fully
signed."
Somehow, I don't understand why the webserver is looking for the resource in the App_GlobalResources instead of directly in the assembly where it is used... or how I could go around this in a clean way. Any hint or idea?
Thanks!

My C# and DLL Data Woes

I'm trying to see if it is possible to pull data from a DLL. I did some research and found that you can store application resources within a DLL. What I couldn't find, was the information to tell me how to do that. There is a MS article that explains how to access resources within a satellite DLL, but I honestly don't know if that is what I'm looking for. http://msdn.microsoft.com/en-us/library/ms165653.aspx I did try some of the codes involved, but there are some "FileNotFoundExceptions" going on.
The rest of the DLL information is showing up: classes, objects, etc. I just added the DLL as a resource in my Visual Studio Project and added it with "using". I just don't know how to get at the meat of it, if it is possible.
If dlls are .net, you can use reflection.
Using System.Reflection;
....
Assembly A= Assembly.LoadFrom(YouDllFileName);
string[] STA;
STA= A.GetManifestResourceNames();
// STA contains all the resource names in the dll
...
// to extract them
Stream str= A.GetManifestResourceStream(STA[I]);
// then, you can make that stream became a file if you wish
You can also extract a .net assembly resources by using ildasm
I'm not totally sure what you might be running into based on your description, but a couple of general pointers...
If what you are trying to find is files you've added to the project you do this:
Right click on the resource in solution explorer, hit properties and set the "Build Action" to "Embedded Resource".
For strings and icons, add a .resx file to the project and insert them in there. If that's what you're doing and still running into issues, check the Build Action.
There is two types of dll.
Managed dll - dll that writen on any .net language (like csharp)
The link that you provide is working with managed dlls.
Unmanaged dll - classic c/cpp dll.
in this case you need to link between managed (your code) and unmanaged.
To find what the type of your dll, you need to add this dll as reference.
In visual studio open project, right click on references(in Solution Explorer).
Then "add reference"->browse-> add your dll.
Then at references, you can see your dll.
Right click on him, and add view in Object Browse.
If you see something like class "c" inside namespace "b", you working with managed dll.
In Object Browser you can learn a lot about your dll (maybe this is more important, than just extract resources)
At this point you can do the way that "Daniel Dolz" answer to you.
Since you say you are able to add the DLL in a using directive you can probably use the methods that the DLL exposes. If you do not have the documentation for the DLL then you may just have to try using the object browser to see what it has to offer.
assume:
using MyDll;
you should them be able to call the methods like this:
string x = MyDll.SomeType.GetValue();
is that what you were asking?

How can I retrieve an embedded xml resource?

I added an XML file as an embedded resource in my class library by using the accessing the project properties in Visual Studio and then Resources | Add Resource | Add Existing File...
I've tried to access the file using the following code, but I keep getting a null reference returned. Anyone have any ideas?
var path = Server.MapPath("~/bin/MyAssembly.dll");
var assembly = Assembly.LoadFile(path);
var stream = assembly.GetManifestResourceStream("MyNamespace.filename.xml");
I find it much easier to use the "Resources" tab of the project's properties dialog in Visual Studio. Then you have a generated strongly typed reference to your resource through:
Properties.Resources.Filename
The MSDN page for GetManifestResourceStream makes this note:
This method returns a null reference
(Nothing in Visual Basic) if a private
resource in another assembly is
accessed and the caller does not have
ReflectionPermission with the
ReflectionPermissionFlag.MemberAccess
flag.
Have you marked the resource as "public" in your assembly?
I found that this article explains nicely how to set this up: http://www.devx.com/dotnet/Article/10831/1954
One thing that may have helped in this case is using a different method for determining the location of the embedded resource.
GetEntryAssembly: Use this method to reference the executable file that was run to start the current process. The entry assembly is set for Windows forms applications, but for most other applications (for example, web projects), GetEntryAssembly returns 'nothing' (or null, in C#).
GetExecutingAssembly: Use this method to reference the same assembly that the executing code is in.
GetCallingAssembly: Use this method to reference the assembly containing the code that called the current method. This method is useful if you write generic code to read embedded resources that is inside a shared assembly where the embedded resource is in the calling assembly.
I personally prefer this method over using the "Resources" tab because it allows me to use other tools to add resources to the project for inclusion on compilation. I don't have to use the GUI with this tool

Categories

Resources