john
john

Reputation: 1282

Unable to load DLL in C#

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

Upvotes: 6

Views: 38282

Answers (5)

Will Dean
Will Dean

Reputation: 39500

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.


Since you seems to be doing dev work, you can also look at the output of dumpbin:

dumpbin /DEPENDENTS <my.dll>

Upvotes: 13

Bence V&#233;gert
Bence V&#233;gert

Reputation: 788

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

Upvotes: 0

Peter C
Peter C

Reputation: 2287

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

Upvotes: 1

john
john

Reputation: 1282

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

Upvotes: 2

Alex Mendez
Alex Mendez

Reputation: 5150

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

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

Upvotes: 1

Related Questions