NET reference dll "cannot find the specified module" solution

Recently, I continued to use ASP.Net to redevelop ACM's Online Judge system. Because of the need to monitor the process, I wrote an unmanaged DLL for ASP.Net to call. I used the development environment of VS2005, and later found out that after using [DllImport("Judge.dll")], the DLL "Judge.dll" could not be loaded and the specified module could not be found. I copied Judge.dll to the Bin directory. But it still prompts that the DLL cannot be found. When adding the DLL reference to the project, I found that adding this unmanaged DLL would cause VS2005 to exit abnormally. @"C:/OJ/Bin/Judge.dll")] In this way, specify the absolute path of the DLL to load normally. There is also a solution here. Net/thread/1121085.aspx">http://forums.asp.Net/thread/1121085.aspx This problem most often occurs when using third-party unmanaged DLL components, mine also The same is the problem at this time. The official solution of Asp.Net Team is as follows: First, you need to confirm which components you reference, which are managed, and which are unmanaged. Managed ones are easy to handle, and those directly used need to be quoted. Indirect use needs to be copied to the bin directory. Unmanaged processing will be more troublesome. In fact, you copy to bin will not help, because the CLR will copy the file to a temporary directory, and then run the web there, while the CLR only The managed file will be copied, which is why we clearly put the unmanaged dll in the bin but still prompt that the module cannot be loaded. The specific method is as follows: First, we can find a place on the server to create a new directory, if it is C:/ DLL Then, in the environment variable, add this directory to the Path 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 themselves, such outstanding is not a solution. However, if we use virtual space, we cannot register the PATH variable or put our own DLL. Copy to the system32 directory. At the same time, we do not necessarily know that DllImport can only use string constants in the physical path of our Dll, and cannot use Server.MapPath (@"~/Bin/Judge.dll") to determine the physical path. After a lot of research, finally came up with a perfect solution. First of all, 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); Get the address of LoadLibrary and GetProcAddress functions respectively, and then get our DLL through these two functions The function inside. We can first use Server.MapPath(@"~/Bin/Judge.dll") to obtain the physical path of our DLL, then use LoadLibrary to load, and finally use GetProcAddress to obtain the address of the function to be used. The following custom class The code completes the loading and function call of LoadLibrary: public class DllInvoke {[DllImport("kernel32.dll")] private extern static IntPtr LoadLibrary(String path);

Guess you like

Origin blog.csdn.net/wangshengfeng1986211/article/details/6567730