Unable to load DLL in C#

5

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

c#
dll
dllimport
asked on Stack Overflow Feb 16, 2012 by john • edited Apr 16, 2019 by Sabith

5 Answers

12

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.

answered on Stack Overflow Feb 16, 2012 by Will Dean • edited Feb 17, 2012 by Will Dean
2

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

answered on Stack Overflow Feb 17, 2012 by john
1

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

answered on Stack Overflow Feb 16, 2012 by Peter C • edited May 24, 2013 by bluish
1

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

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

answered on Stack Overflow Feb 17, 2012 by Alex Mendez • edited May 23, 2017 by Community
0

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

answered on Stack Overflow Jan 29, 2021 by Bence VĂ©gert

User contributions licensed under CC BY-SA 3.0