?
Solved

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

Posted on 2011-09-10
13
Medium Priority
?
591 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 75

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 75

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 75

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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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 75

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 75

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 75

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 75

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

830 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