Solved

Using DLLs added as a resourcein C#

Posted on 2012-03-18
9
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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