Why can't my application find a dependent dll in the same directory?

c#, c++-cli, dll, interop

Solution

or one of its dependencies

Did you deploy the CRT runtime DLLs on that machine? Make sure to deploy the Release build of your assembly. The debug version of the CRT is not redistributable.

Problem

I have a simple console test application, `ConsoleApplication1.exe`, which references another assembly `clipper.dll`. On 3 machines that I've tested it on, it is possible to place both files in e.g. `c:\test\`, and execute `ConsoleApplication1.exe`. On one other machine, which happens to be a client machine, running `ConsoleApplication1.exe` results in a program crash and the following being printed to the console: ``` C:\test>dir Volume in drive C has no label. Volume Serial Number is 7C46-414F Directory of C:\test 07/12/2010 06:08 PM <DIR> . 07/12/2010 06:08 PM <DIR> .. 07/12/2010 05:13 PM 11,776 ClassLibrary1.dll 07/12/2010 05:13 PM 30,208 ClassLibrary1.pdb 07/12/2010 04:55 PM 3,572 ClassLibrary1.tlb 19/11/2010 02:46 PM 235,008 clipper.dll 19/11/2010 02:46 PM 1,534,976 clipper.pdb 07/12/2010 05:13 PM 6,144 ConsoleApplication1.exe 07/12/2010 05:13 PM 11,776 ConsoleApplication1.pdb 01/08/2010 12:52 PM 139,264 nunit.core.dll 01/08/2010 06:41 AM 57,344 nunit.core.interfaces.dll 01/08/2010 06:41 AM 135,168 nunit.framework.dll 01/08/2010 06:41 AM 547,262 nunit.framework.xml 11 File(s) 2,712,498 bytes 2 Dir(s) 477,821,784,064 bytes free C:\test>ConsoleApplication1.exe Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass embly 'clipper.dll' or one of its dependencies. The specified module could not b e found. at ConsoleApplication1.Program.Main(String[] args) ``` This is a odd to me, although I don't understand dll load rules comprehensively I thought it would search the CWD for the specified file. And the fact this same setup works on the various other computers I've tried is also odd. Curiously, on a working computer if I remove clipper.dll and then run it: ``` C:\Temp>ConsoleApplication1.exe Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass embly 'clipper, Version=1.0.3975.26584, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. at ConsoleApplication1.Program.Main(String[] args) ``` The unhandled exception is slightly different, with a full assembly name. - Both `ConsoleApplication1.exe` and `clipper.dll` are built for .NET 4. - All machines have the .NET 4 runtime installed. The problem machine does not have the .NET sdk - clipper.dll is a combination of ummanaged c++ code and C++/CLI. It is a suspicious character because it is basically the first C++/CLI .NET assembly I have created and much fumbling was involved. It seems to load in reflector fine. - These efforts came about when I was trying to figure out why I was unable to successfully utilise a COM object (call it `foo.dll`) which also referenced `clipper.dll` - all also on the same problem machine. After several hours of regasm hell I was able to further isolate it to this.

Original source