Unable to load DLL. Unable to find the solution for the specified Module

Source: Internet
Author: User

Original article published on 19:28:25

I'm trying to write it with ranorex today.TestThe script encountered a problem and foundDLLThe component cannot be called, causing an exception. You can search for three versions of the solution on the network:

Solution 1

Copy XXXX. dll (the component cannot be found) to the bin directory in the project folder.

Solution 2

Copy XXXX. dll (the component cannot be found) to the System32 directory.

Solution 3

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

Recently, I continued to use ASP. NET to re-develop ACM's online judge system. Because process monitoring is required, I wrote an unmanaged DLL for ASP. NET to call.
I used the vs2005 development environment. Later I found that the [dllimport ("judge. dll")] prompts that the DLL "judge. dll" cannot be loaded and the specified module cannot be found.
In this case, I set judge. DLL copied to the bin directory, but still prompts that the DLL cannot be found. When you add a DLL reference to the project, if you find that adding this Unmanaged DLL will cause vs2005 to exit abnormally (the same problem is found after searching the Internet)
Later I found that [dllimport (@ "C: \ OJ \ bin \ judge. dll")]
In this way, the absolute path of the specified dll can be loaded normally.
There is also a solution here. Net/thread/1121085. aspx "> http://forums.asp.Net/thread/1121085.aspx
This problem occurs most often when a third-party Unmanaged DLL component is used. I also encountered this problem. The official solution of ASP. NET team is as follows:
First, you need to confirm which components you reference, which are hosted and which are not managed. it is easy to host and directly referenced by the user. for indirect use, you need to copy it to the bin directory. non-hosted processing will be troublesome. in fact, copying to the bin does not help, because the CLR will copy the file to a temporary directory and then run the Web, while the CLR will only copy the hosted file, this is why we obviously put the unmanaged DLL under the bin but still prompt that the module cannot be loaded.
The procedure is as follows:
First, create a directory somewhere on the server, for example, C: \ DLL.
Then, add the directory to the PATH variable in the environment variable.
Finally, copy all the unmanaged files to c: \ DLL.
Or simply put the DLL in the System32 directory.
For applications that can be deployed by yourself Program This is not a solution. However, if we use a virtual space, we cannot register the PATH variable or copy our own DLL to the System32 directory. At the same time, we do not necessarily know the physical path of our DLL.
Dllimport can only use string constants, but cannot use server. mappath (@"~ /Bin/judge. dll ") to determine the physical path.
After a thorough research, I finally came up with 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 );
Obtain the addresses of the loadlibrary and getprocaddress functions respectively, and then use these two functions to obtain the functions in our DLL.
We can use server. mappath (@"~ /Bin/judge. dll ") to obtain the physical path of our DLL, load it with loadlibrary, and finally use getprocaddress to obtain the function address to use.
The Code Complete loadlibrary loading and function calling:
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 to be executed to the Delegate
Public Delegate invoke (string apiname, type T)
{
Intptr API = getprocaddress (hlib, apiname );
Return (delegate) Marshal. getdelegateforfunctionpointer (API, t );
}
}

The following code calls
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); // call the compile function defined in my DLL.
========================================================== ========================================================== ===

PS: At first, I added a [stathread] to the front of the main method, and then followed the solution one. However, I am writing a test script, and there is no main method in it, I thought I could keep the old path and write it before testinitialize, but the fact tells me that I was wrong and I had to continue searching on the Internet. A guy caught a solution, is the omnipotent solution 3 ^ _ ^

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.