C # could not load DLL could not find the specified module

Source: Internet
Author: User

A DLL component cannot be called to cause an exception, and a search on the network finds three versions of the solution:

Programme I

Copy XXXX.dll (the component that you are not prompted for) to the bin directory in the project folder

Programme II

Copy the XXXX.dll (components that are not found) to the System32 directory

Programme III

==================================== Original ===========================================

Recently, we continued to re-develop ACM's online judge system with ASP. Because of the process monitoring, I wrote an unmanaged DLL for ASP.
I used the VS2005 development environment and later found that using [DllImport ("Judge.dll")] prompted the DLL could not be loaded "Judge.dll" Cannot find the specified module
I then copied the Judge.dll to the bin directory, but still prompted not to find the DLL, in the project to add the DLL reference, found that adding this unmanaged DLL will cause the VS2005 to exit unexpectedly (Internet search also found that someone has the same problem)
Later found with [DllImport (@ "C:/oj/bin/judge.dll")]
This way, the absolute path of the specified DLL can be loaded normally.
Here's another solution. Net/thread/1121085.aspx ">http://forums.asp.net/thread/1121085.aspx
This problem most often occurs when using third-party unmanaged DLL components, and mine is also the problem, the official solution for ASP. NET team is as follows:
First you need to make sure which components you refer to, which are managed, and which are unmanaged. Managed well, directly used needs to be referenced, indirectly used by the need to copy to the bin directory. Unmanaged processing can be cumbersome. In fact, you don't have any help copying to the bin, Because the CLR copies the files to a temporary directory and then runs the web, and the CLR only copies the managed files, that's why we put the unmanaged DLLs under the bin but still hint that we can't load the modules.
The following are the specific practices:
First, we find a place in the server to create a new directory, if the C:/dll
Then, in the environment variable, add this directory to the PATH variable
Finally, copy all the unmanaged files to C:/dll.
or more simply put the DLL in the System32 directory
This is not a workaround for applications that can be deployed on their own, but if we are using virtual space, we cannot register the path variable or copy our own DLL to the System32 directory. And we don't necessarily know the physical path of our DLL.
DllImport can only use string constants, not Server.MapPath (@ "~/bin/judge.dll") to determine the physical path.
After a study, I finally thought of a perfect solution.
First we use
[DllImport ("kernel32.dll")]
private extern static IntPtr LoadLibrary (String path);
[DllImport ("kernel32.dll")]
private extern static IntPtr GetProcAddress (IntPtr lib, String FuncName);
[DllImport ("kernel32.dll")]
private extern static bool FreeLibrary (IntPtr Lib);
The addresses of the LoadLibrary and GetProcAddress functions are obtained, and the functions within our DLLs are obtained by using these two functions.
We can use Server.MapPath (@ "~/bin/judge.dll") to get the physical path of our DLL, then load it with LoadLibrary, and finally use GetProcAddress to get the function address
The following code for the custom class completes the LoadLibrary mount and function call:
public class Dllinvoke
{

[DllImport ("kernel32.dll")]
private extern static IntPtr LoadLibrary (String path);
[DllImport ("kernel32.dll")]
private extern static IntPtr GetProcAddress (IntPtr lib, String FuncName);
[DllImport ("kernel32.dll")]
private extern static bool FreeLibrary (IntPtr Lib);
Private INTPTR hlib;
Public Dllinvoke (String dllpath)
{
Hlib = LoadLibrary (DllPath);
}
~dllinvoke ()
{
FreeLibrary (hlib);
}
Convert the function you want to execute to a delegate
Public Delegate Invoke (String apiname,type t)
{
INTPTR API = GetProcAddress (hlib, apiname);
Return (Delegate) marshal.getdelegateforfunctionpointer (api,t);
}
}

Called by the following code
public delegate int Compile (String command, StringBuilder inf);//Compile
Dllinvoke dll = new Dllinvoke (Server.MapPath (@ "~/bin/judge.dll"));
Compile Compile = (Compile) DLL. Invoke ("Compile", typeof (Compile));
StringBuilder inf;
Compile (@ "gcc a.c-o a.exe", INF); This is the compile function defined in the call to my DLL.

--------------------------------------------------------------------------------------------------------------- ----

C # could not load DLL could not find the specified module (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.