C# DllImport call throws an exception on one computer, but works on another

0

Upon running the following C# application which references an unmanaged DLL (written in C), I get an DllNotFound exception with the following information:

Additional information: Unable to load DLL 'C:\Windows\System32\myLib.dll': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)

What I've tried:

  • I've verified that the DLL is present in the expected path (in fact, I get a different error message when the DLL is not there)
  • I've copied the exe and accompanying dll to another computer (in the same path), and it works without issue.
  • I've copied the DLL to the System32 directory (and referenced it there) as suggested by certain other answers here.
  • I've double-checked the Access rights, and disabled MS Security Essentials.
  • I've ensured the platform targets match.

This is baffling as there appear to be so few dependencies required to load a DLL.

c#
c
dll
dllimport
asked on Stack Overflow Sep 28, 2016 by firetiger77

2 Answers

3

Don't copy your DLLs to the system folder. That belongs to the system and you should not modify it. Remove those DLLs from any system folders they have been placed in.

Instead put your DLLs in the same directory as the executable. After that the other step that is needed is to make sure that any dependencies, typically the VC runtime, are available. Depending on exactly how you want to deploy this will likely involve installing the VC redistributable package on any target machine.

answered on Stack Overflow Sep 28, 2016 by David Heffernan
0

An older version of a dependency (a Jungo DLL in this case), which did not export the same set of functions as the newer version, had been installed to the machine with the error. Copying the newer version over solved the problem.

So, while the dependent file was present on both machines, the necessary set of exported functions were not. C# specified a "procedure could not be found" error on DLL A, when technically, it was on a dependency of a dependency.

Dependency walker proved useful in identifying what the dependencies were in the first place and indicating that there was a missing import in one of them.

answered on Stack Overflow Sep 30, 2016 by firetiger77

User contributions licensed under CC BY-SA 3.0