Solved

Using DLLs added as a resourcein C#

Posted on 2012-03-18
9
314 Views
Last Modified: 2012-03-22
I want to use a DLL I made in C# (I believe that means it is managed code) in a project I'm doing, but I want a single EXE after building. I added the DLL as a resource, but don't know how to access the objects or functions in the DLL.
0
Comment
Question by:byt3
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 37736967
Using DLL Component as Embedded Resource (Using Reflection)
http://www.codeproject.com/Articles/15253/Using-DLL-Component-as-Embedded-Resource-Using-Ref
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37737251
You should be able to use the DLL as an Embedded Resource, and reflection to call the Assembly.GetManifestResourceStream, which will return the assembly in a stream.  Use a stream reader to get the assembly bytes.  

Load the assembly, using the Assembly.Load(byte[]) method:

Assembly.Load Method (Byte())
http://msdn.microsoft.com/en-us/library/h538bck7.aspx
0
 
LVL 2

Author Comment

by:byt3
ID: 37737894
Thank you for the posts, I'll try this out later today to make sure I can understand the links you sent me.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 37738387
Microsoft as a tool to merge 2 assemblies (a .exe is an assembly, a .dll is an assembly) into one. You will find it there:

http://www.microsoft.com/download/en/details.aspx?id=17630
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 37739130
Keep in mind that loading an assembly with reflection, you would need to use either more reflection to execute the methods, or dynamic keyword (C#).  IL Merge is a good suggestion, but can be very messy if you have a lot of references, since everything needs to be bound statically when merging at the IL level.  I have seen a simple set of references balloon out to hundreds of indirect assemblies.
0
 
LVL 40
ID: 37739386
An easier solution could be to simply bring the code source of your dll into the application project.

As for most techniques in programming, this is really a project by project decision. Some projects will work better with one solution, some projects will be better of with another technique. You have to see how your stuff was built, how you want to do it and also take into consideration the future maintenance of the project.

One question that has not been asked however, is why do you really want to do that?

If somebody took the decision of putting those classes in a dll first, they had a reason. If you want to send everything as one unit, there is also a reason. Sometimes, the decisions take in regard to go exe+dll vs exe only are taken because the designer does not know of certain techniques. For instance, I see a lot of programmers that wants a merge simply because they see a security problem by sending a dll, thinking that everything must be Public in a dll in order to be used from outside. This is not true.
0
 
LVL 2

Author Comment

by:byt3
ID: 37747840
Dhaest,
I don't understand Reflection enough to be able to work with that link I think.  If there is a link you can provide to explain it I'd be happy to read and hopefully figure this out.
0
 
LVL 2

Author Comment

by:byt3
ID: 37747899
TheLearnedOne,
I did find an article explaining what you're talking about I think:
http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

The thing is I can't initialize an object in my code from that dll and have it compile without errors being thrown:

The type or namespace name 'ClassInDLL' could not be found ...

I must have to additionally use reflection or something to create an instance of that object  and build it or something...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37748870
You should investigate the ILMerge suggestion, also, as it can definitely be a viable solution for a small number of external assembly references.  You should be able to reference the assembly as if it were external, but it would be internal to your executing assembly.
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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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