5
votes

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: exception screenshot

5
Probably obvious question, but the dll is of course actually present?Lasse V. Karlsen
yes it's present. in fact if try to add a reference and i choose the dll as a target. i face this error: a reference to c:\...\reader.dll could no be addedjohn
You can't add a reference to the dll because it is not a .NET assembly. It has nothing to do with your issue. Try using dependency Walker to debug your issue dependencywalker.comTerkel
Is the reader.dll a c/c++ library? Does it have any dependencies itself that perhaps it cannot find? Is it a com library that can be registered and if so does it register?Alex Mendez
@Alex GPSVC = Group Policy Servicewpearse

5 Answers

12
votes

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.

2
votes

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

1
votes

I found this in another post. Maybe it will help your situation

NUnit "missing" GPSVC.DLL on Windows 7/64

1
votes

If it's a simple C DLL it just needs to be in the same folder as the .exe.

0
votes

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:

https://support.microsoft.com/en-us/topic/the-latest-supported-visual-c-downloads-2647da03-1eea-4433-9aff-95f26a218cc0