Solved

Including .dll files into compiled .EXE (C#)

Posted on 2011-09-10
13
519 Views
Last Modified: 2012-05-12
I can't seem to wrap my head around this. I have 2 dll files that I would like to include in my project so that they are not required to be distributed with the application. I also don't want these dlls to be written to the execution directory at run-time if possible.

I've added the dlls to Resources but at this point I'm stuck.

How do I make use of these dlls at runtime? It doesn't seem possible to add a Reference from a local Resource...
0
Comment
Question by:mcainc
  • 8
  • 3
  • 2
13 Comments
 
LVL 40
ID: 36518144
Resources are used for everything that is not code (strings, images, sounds, reports). They cannot contain code, compiled or not.

If you want dlls to be incorporated in your application, you first need Visual Studio 2010. This option was not available in previous versions.

And only COM dlls can be included. .NET dlls must be either already installed in a proper location on the users computer or distributed with the application.

In order include a COM dll in your application in Visual Studio 2010, you add the dll to your References, not your Resources. References can be added with a right click on the Reference item in the Solution Explorer.

Once the Reference has been added to the project, select it and set its Embed Interop Types to True in the Properties window.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36519073
Actually, it is possible to load a DLL as a compiled resource. It makes it a tad more difficult to use, but it is possible. You wouldn't be adding references to the DLL at that point, IIRC. You would instead use dynamic loading of the library.
0
 
LVL 40
ID: 36519138
@kaufmed

Could you at least give a hint on how you could prevent having to distribute the dll with the application through dynamic loading?

What I know as dynamic loading enables you to link with a dll that was not referenced. It does not include the dll into the .exe at compilation.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36519156
Yes. I planned to shortly. I'm at my Linux box at the moment, so it's rather a hindrance to try and do so from that environment  ; )
0
 
LVL 40
ID: 36519188
I too work in different environments and languages. It's very easy to get confuse while working, even more so here where we something answer in a rush.

I know the feeling :-)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36519298
All, please have a look at the linked project. I have presented two approaches to using the resource in code--the first being less preferrable, but the latter requiring a slight tweak to existing code.

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36519322
P.S.

I noted in my comments that you would rely on reflection for the first example, and this seems to imply the second approach does not use reflection. The use of Activator.CreateInstance makes use of reflection. The benefit in the second approach is that since both assemblies have a commonly-referenced assembly, namely Common.dll, you can treat the dynamically loaded object as being the interface, and as such, you can use the methods defined by the interface statically in code. This is demonstrated in the call:

Console.WriteLine(iLibrary.GetWelcomeMessage());

Open in new window


where GetWelcomeMessage is defined by the interface, so we know about this method at compile time and are permitted to use it statically.
0
 

Author Closing Comment

by:mcainc
ID: 36520488
wow thank you this helps tremendously!!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36520518
NP. Glad it helped  = )
0
 

Author Comment

by:mcainc
ID: 36520538
So just to clarify...

I will distribute a "common.dll" file that is actually only making references to the actual DLL which we've embedded into the applications Resources? So the real DLL is essentially packed into the application / hidden?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36520648
I will distribute a "common.dll" file that is actually only making references to the actual DLL which we've embedded into the applications Resources? So the real DLL is essentially packed into the application / hidden?
It's not making a reference to the "hidden" DLL. The hidden DLL contains a class declared to implement the interface ILibrary (in my example). Since that class implements the interface, you can cast an instance of it to type ILibrary (that's the as ILibrary part). Since the main project knows about ILibrary classes, you can work with an ILibrary even though at compile time your main project knows nothing of the types contained by hidden DLL. At compile time of your library, you set up your library to implement the appropriate functions defined by ILibrary. Later, at runtime, when you load the DLL into the app domain, .NET will know that your library implements ILibrary, and this is why you can safely cast to ILibrary once you create an instance using Activator.CreateInstance.

One "problem" I ran into is that when I initially added the DLL as a resource, I hadn't yet included the logic to implement the ILibrary interface. I later modified the code accordingly, but I forgot to update the resource. When I got to the line that casted the instance to type ILIbrary, the cast failed. This was because the original DLL wasn't defined to implement ILibrary. Make sure when you add your resource that you are adding the most up-to-date version of the DLL to prevent subtle errors similar to what I encountered  = )

I forgot to mention:  the Resources folder of my example shows two resources, but that is in error. I mistakenly added the wrong file. I deleted from the Properties->Resources page, but I forgot that it would still show in the Resources folder.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36520653
To clarify a bit more, the only references that occur in the sample are Library referencing Common.dll and Main referencing Common.dll. This is to grant each respective project compile-time knowledge of what the ILibrary is defined as. Main knows nothing of Library's internal code in the sample. Main only knows about ILibrary's code. The most Main knows about Library is that it is a resource and, at the cost of simplicity of the demo, there is a namespace and class called "_27301950_Library.LibraryClass". This is hard-coded into Main's code and there's probably a more elegant way of handling this.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36520655
I forgot to mention:  the Resources folder of my example shows two resources, but that is in error. I mistakenly added the wrong file. I deleted from the Properties->Resources page, but I forgot that it would still show in the Resources folder.
The resource in error for that folder is 27301950_Common.dll.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now