how to load a dll in a c# project
error:
Unable to load DLL 'Reader.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
code sample:
[DllImport("Reader.dll")]
public static extern byte OpenReader(ref IntPtr hCom, byte LinkType, string com_port);
image:
This error has the following causes and solutions: The file isn't on the proper path. Ensure the DLL is on the Windows System path. The DLL is corrupted or was deleted.
If you are receiving this error message at the startup of the computer, place the computer in clean boot state and check if any third-party program is causing this issue. Putting your system in clean boot state helps in identifying if any third party applications or startup items are causing the issue.
A DLL is a library that contains code and data that can be used by more than one program at the same time. For example, in Windows operating systems, the Comdlg32 DLL performs common dialog box related functions. Each program can use the functionality that is contained in this DLL to implement an Open dialog box.
If the problem is really "cannot be found", then using ProcMon from Sysinternals will show you where the system is looking for the DLL.
However, often these sort of exceptions mean 'I found the DLL but I can't load it', and that can be because a dependency of the DLL is missing rather than the DLL itself, or because the DLL is incompatible with the app trying to load it. If your C# app is set for 'Any CPU' and you're on a 64bit machine, you'll get this sort of error loading unmanaged 32-bit DLLs.
One way to isolate the problem would be to create a simple C/C++ project which loads the DLL. (Load it dynamically with LoadLibrary if you don't have access to the import lib.) Then use Dependency Walker to profile the test harness, and it will report the names of missing DLLs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With