Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to load DLL in C#

Tags:

c#

dll

dllimport

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

like image 838
john Avatar asked Feb 16 '12 23:02

john


People also ask

Why is DLL not loading?

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.

How do I fix Hresult 0x8007007E problem?

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.

What is DLL file in C#?

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.


1 Answers

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.

like image 136
Will Dean Avatar answered Sep 19 '22 13:09

Will Dean