Hello Everyone,
I have a window application that written in C# and I want to use one of the dll file from the API c/c++ to my window project. I use the P/Invoke to import the dll file and I initialize the declaration of each function call afterward. In the code below, I have a function to read the access database and it returns 2 values, so I use those 2 values to pass parameter to one of the function call in the dll, but when it reaches to the Init(); function, it fails because of "Unable to load DLL 'elevation.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) ", I put the elevation.dll and all its related dll/files to the bin folder. I try serveral ways that I could find the answer on web to solve this problem, but unsuccessful. I'm hoping anyone in here knows this problem and help me to solve it? I'm really aprreciated and thankyou.
I develope this application in Visual Studio 2005 and below is my DllImport and a function call:
[DllImport("elevation.dll", SetLastError =
true)] private static extern void Init();[DllImport("elevation.dll", SetLastError =
true)]private
static extern bool GetElevation(double dLat, double dLon, ref double dHeight);
//Read .mdb to get the Latitude and Longitude, and pass 2 value parameter to the "GetElevation" function.
while
(myReader.Read()){
Latitude =
Convert.ToDouble((myReader["lat"] + ""));Longitude =
Convert.ToDouble((myReader["longi"] + ""));Init(); //Failed to load the elevation.dll at the Init(); function
GetElevation(Latitude, Longitude, ref dHeight)
}
AlanPosted Apr 4, 2008, 6:51 AM
I wonder whether this problem could be related to the one described here which occurs when the VS 2005 debugger is running a managed application which uses a dll containing version 8 of the MFC class libraries:
http://support.microsoft.com/kb/913996
You did mention version 8 of the C runtime library but, as you said it was a C/C++ API, are you using MFC as well?
It would be interesting to see if this problem goes away if you use a release rather than a debug build.
Jenn HuynhPosted Apr 3, 2008, 4:18 PM
Thankyou Alan for replying and for your help.
I did have a code to check if the elevation.dll file exist which it does and it goes into a loop but it hits the Init(); function, it failed with the new error message below.
Managed Debugging Assistant 'LoaderLock' has detected a problem in 'D:\Elevation\Elevation\bin\Debug\Elevation.exe'.
Additional Information: Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
I try to unchecked the 'LoaderLock' in the Managed Debugging Assistant in the Exception, and I got the new error message: An application has made an attempt to load the C Runtime Library incorrectly. I have no idea what it is and how to fix this.
Also, I take your suggestion and create a small window application with one one control button for the event handler. In that event handler is just import dll file and a function call to it, but still failed with the first error message above: Managed Debugging Assistant 'LoaderLock' has detected a problem in 'D:\Elevation\Elevation\bin\Debug\Elevation.exe'.
I ran elevation.exe and it worked fine, so I think there shouldn't be a problem with the dll file. So I'm wondering is my import dll code wrong or there is some components missing that I really don't know which one. I've been trying to solve this problem, but unable to solve it :-(
Below is my import dll, declaration and a function call in the ImportDll, do you or anyone have any ideas how to solve those two error message above? thanks
public partial class Form1 : Form
{
//Import "elevation.dll"
[DllImport(@"D:\Elevation\Elevation\elv\elevation.dll")]
}
private void ImportDll_Click(object sender, EventArgs e)
{
if (File.Exists(@"D:\Elevation\Elevation\elv\elevation.dll") == true)
{
Init(); //Failed: Managed Debugging Assistant 'LoaderLock' has detected a problem
}
}
AlanPosted Apr 3, 2008, 10:55 AM
TBH, Jenn, I'm baffled :-/
If it wasn't for the fact that this worked OK in your web application, I'd be thinking that there was something wrong with the dll itself or the Init() function had been exported with a different, possibly mangled, name
Just as a matter of interest, if you do a File.Exists() on the dll, does it find it then?
What may help with this seemingly intractable problem is to come right out of VS, copy all the relevant dlls into a new folder and add that folder to your PATH environment variable.
I'd then go back in again and try to build a minimal test application which does nothing but access the dll. If you're lucky it might work or at least give you can insight into what the cause could be. You could also see if it makes any difference if you compile the minimal app from the command line rather than from inside VS.
Jenn HuynhPosted Apr 3, 2008, 9:27 AM
Hi Alan,
I downloaded the "Dependency Walker" program and opened the elevation.dll file. There was 2 dll files missing: msvcr80.dll and dwmapi.dll. I download it and put it in along with all elevation.dll files and run my program, I still have the same error message: "Unable to load DLL". The specified module could not be found (exception from HRESULT: 0x8007007E). I also try to put those 2 files in the system32, but still not working. So I'm wondering what cause it not to load the dll file in the window application from another API c/c++. It works in the web application for me with the same environment in Visual Studio 2005 but when I try to implemented it in the window application and it failed to load the dll. I also tried to set the window platform from "Any CPU" to "x84" but still no help.
AlanPosted Apr 3, 2008, 5:17 AM
If it doesn't work when you specify the full path, then all I can think of is that elevation.dll itself depends on another dll or dlls which, for some reason, the system can't now find.
I know you said that you'd copied all the related dlls/files into the same folder but I'd check out elevation.dll with the free Dependency Walker tool to make sure there's nothing you've overlooked:
http://www.dependencywalker.com/
You can,of course, ignore system dlls such as user32.dll, kernel32.dll etc which will be contained in folders within the scope of your PATH environment variable.
Jenn HuynhPosted Apr 2, 2008, 4:19 PM
Thanks Alan for replying,
If I put the dll files in the bin\Debug folder where the .exe is, it still failed to: Error: Unable to load DLL 'elevation.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
And if I specified the full path of dll file as below, I still have the same error.
[DllImport(@"D:\Elevation\Elevation\bin\Debug\elevation.dll")]
I just finished one web application and using the same method to import the elevation.dll file into a project. It works fine and it would be able to load the dll file and call the function in the dll. But when I try to develope one in window application with the same environment in Visual Studio 2005 and it fails to load the dll file when call one of the function. Any ideas why it happens?
AlanPosted Apr 2, 2008, 3:56 PM
Rather than put the unmanaged dlls in the \bin folder itself, I'd put them in its \debug sub-folder so that they are in the same folder as the exe.
If you still get the error, I'd try using the full path in the DllImport attribute.