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:
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.
Although the reader.dll is unable to load GPSVC.dll and IESHIMS.DLL. i managed to make it work by running the corflags command on application.exe the application is now marked as 32bit:
corflags application.exe /32bit+
Version : v4.0.30319
CLR Header: 2.5
PE : PE32
CorFlags : 3
ILONLY : 1
32BIT : 1
Signed : 0
If it's a simple C DLL it just needs to be in the same folder as the .exe.
I found this in another post. Maybe it will help your situation
For me the solution was to installing the C++ Redistrable X64 on client machine. (Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019.)
The dll was already on the right place, in the same folder than the .exe file.
Here you found the download link:
User contributions licensed under CC BY-SA 3.0