Search code examples
c#.netdlldllnotfoundexception

Why might I get a DLL not found exception on Vista but not XP?


I have an app that relies on several managed libraries. These managed libraries in turn rely on some unmanaged libraries.

When I deploy the app to a machine running XP, it runs fine. When I do the same on a machine running Vista, I get a DLL not found exception.

I've tried both a VS2010 setup project and an NSIS installer to do the deployment and it's the same in both cases.

Why might this happen? What can I do to get around it?


Update - Further details

  1. Both installers check for the installation of .NET 4.0 and install it if need be
  2. The Vista computer is 64 bit, but the installation gets directed to the x86 program files folder as expected
  3. In both cases I have an admin account
  4. The DLLs are kept in the same directory as the executable
  5. As far as I can tell, the files are getting copied to the right directory

Update 2

  1. The full error is at http://pastebin.ca/2046487
  2. The DLL is Audiere.Net.dll, which is one of mine and is a managed library.

I'm not sure if that error means that it can't find Audiere.Net.dll, or whether it can't load it because one of it's dependencies can't be found.


Update 3 - Stuff from Process Monitor

After running process monitor (thanks Mehrdad!) there are several entries which don't have a status of "SUCCESS". Some of these are "NAME NOT FOUND" and some are "PATH NOT FOUND". (It's even querying the PDB files, which I had thought were only used by a debugger.) It's really hard to see which entries might be the one leading to the actual failure. Anyway, I've uploaded the log (filtered to have a relevant path) in case it means something to anyone.


Update 4 - Added .pdb files

So I kinda got desperate and included the .pdb files to the output of the installer. I thought it would be useless, but it actually resulted in a more useful error. Rather than simply saying DLL not found, I now get a BadImageFormatException. Googling this tells me that this is a common problem for binaries compiled on x86 but being run on x64 (as the Vista machine is).

The suggested remedy is to force it to target x86, but Audiere.Net.dll already was. Could the fault lie with the library that it wraps?


Solution

  • The solution turned out to be quite straightforward: one of the unmanaged DLLs needed to be recompiled for x64.

    Key steps:

    • Check process monitor for likely sources of error. Look carefully at the error report that Windows offers to send when the app crashes.
    • Include the .pdb files for managed libraries. This seemed to result in more informative error messages.
    • These error messages not only specified which managed library was causing the error, but also indicated that it was a x86/x64 issue. (BadImageFormatException)
    • Following some sound advice, check that all of the unmanaged libraries are targeting x86. (Mine were, but it's good to be sure.)
    • Recompile the unmanaged dependencies of the troublesome managed library on an x64 machine.
    • Write an install script that copies the appropriate (x86 or x64) version of the DLL.
    • Profit!

    Specifics:

    • The problem I had seemed to be with Audiere.Net.dll, but was actually caused by a problem with libaudieresharpglue.dll.
    • I use NSIS for installers. To accomplish the architecture specific DLL, I used a header called x64.nsh.