views:

120

answers:

4

I have a class library project that i have made. Let's call it ClassA. In ClassA i need to access some tools that reside in dll (ToolsDLL.dll).

In ClassA I have added ToolsDLL.dll to the project and selected the ToolsDLL.dll file to Copy To Output directory ALWAYS. So that library builds and compiles just fine and in the output directory i see ClassA.dll along with ToolsDLL.dll

Next, I want to write an application, say App_A that uses the methods in ClassA. So, in my App_A project, I added a reference to ClassA.dll so that I can access it's namespace. All is well and good, it build/compiles.

The problem is as soon as I run App_A and it gets to a point where ToolsDLL.dll needs to be used it throws an exception "Unable to Load ToolsDLL.dll. I don't understand how it is possible that it can't find that dll because it is in the same directory as ClassA.dll.

I found that if i put ToolsDLL.dll in the output directory of App_A it works just fine. Is there any way around that? Is there any way that ToolsDll.dll can be somehow bundled with ClassA.dll. The reason is that my customers will be writing their own applications similar to AppA and it would be nice if they only had to reference one file in their project and not multiple.

+7  A: 

There's a tool from Microsoft called ILMerge. It will probably do what you want, bundling several assemblies into one file.

P.S.: Another, fairly frequently used solution to your problem would be to add a post-build event to your application's solution/project that copies the required ToolsDLL.dll over to the output directory? Something along the line of:

xcopy /y /d $(SolutionDir)\lib\ToolsDLL.dll $(OutputDir)\ToolsDLL.dll

(Sorry if I get some of it wrong, I'm typing this from my memory.)

Of course, your customer would also have to do this. But then again they've probably done this before.

stakx
It looks like ILMerge can only be used on a .Net assemblies. Is that correct. In my case, ToolsDLL.dll was provided to me by a chip manufacture. I don't know if it is a .net assembly or not. Will that matter?
Jordan S
Yes, I believe it only works for .NET assemblies.In order to figure out what kind of `.dll` you've got, perhaps you could use the .NET SDK utility `ildasm`, to try and disassemble the `.dll`. If it's a .NET assembly, this should work; if it contains native code, `ildasm` should fail.
stakx
I noticed in Visual Studio if i go to ToolsDll.dll -> Properties there is a Property called Action. Can I just set this to Embedded Resource?
Jordan S
There should be a "Copy if Newer" or "Copy Always" options, use those.
mletterle
+1  A: 

Have you added the DLL to the project, or have you actually added a reference to it? You should do the latter, then this sort of thing is taken care of automatically for you. It sounds like you have added the actual file to the project files, and set it to copy.

If you do definitely want a single file approach, then accept the others' suggestions of ILMerge obviously...

David M
A: 

You can use ILMerge to combine assemblies into one DLL.

https://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx

Aequitarum Custos
A: 

You can either use an IL merging tool, or install ToolsDLL.dll in the GAC.

Also if you look at the output of App_A, ClassA.dll is already there, that's where it's looking for ToolsDLL.dll.

mletterle