If you are involved in projects where software and hardware come together, you have probably already faced the exception I’m writing in this post about.
In our company, one of my responsibilities is to develop a communication layer between our software and compatible with it hardware. Without digging into details, I can just say that mostly it is a real *.DLL hell… What I have to work with is just a piece of hardware and a provided *.DLL to call hardware commands. Usually I don’t know what is inside this *.DLL. It’s a black box. I know just external interfaces.
I love to use the .NET platform for such kind of cases because of an awesome .NET Interoperability Platform. I can always keep my communications without any changes (Sockets, WCF, LINQ, etc.) and use imports from *.DLL:
using namespace System::Runtime::InteropServices;
[DllImport("SOME_HARDWARE_DLL.dll")]
static void SOME_DLL_METHOD();
One of the problems you can face there is that you never know what is going on inside the *.DLL. Once I got this type of exception:
Unable to load DLL 'SOME_HARDWARE_DLL.dll': The specified module could not be found.
(Exception from HRESULT: 0x8007007E) at …
I thought I’m crazy, because the *.DLL was in the correct place. I did everything I thought I could: placing the *.DLL near the executable, placing it inside Visual Studio Solution folder, inside {WindowsDir}\System directory (System32, SysWOW64), everywhere I could. I set the PATH to every place. Nothing helped. I spent almost 5 hours trying to find any error inside the code. But then I found an extremely helpful utility – Dependency Walker.
And it happened that inside this *.DLL was a call to another *.DLL which was missing on my machine!
It turns out (unfortunately) that in all situations like that one – one *.DLL is dependent on another – the .NET Interop will always give you the same: “Unable to load DLL: The specified module could not be found. (Exception from HRESULT: 0x8007007E)”. Once I installed the required driver (yet another one for that case), everything immediately became just fine.
Conclusion: Check for dependencies if you are working with third-party “black-box” *.DLL.
Thanks and happy DllImporting.
Max.