Using DllImport to load unmanaged dll into managed application
In my project I have an unmanaged native C++ dll and a C# application. I am trying to import a function from the unmanaged dll using DllImport but I keep getting a DllNotFoundException.
Here is my code that calls the DLL.
using System.Runtime.InteropServices;
namespace TestApp
{
public delegate void UpdateDelegate(string s);
class Program
{
[DllImport("CGPUnmanagedLibrary.dll")]
internal static extern int parse_raw_gsod_file(
[MarshalAs(UnmanagedType.LPStr)]
string filePath,
int minTemp,
UpdateDelegate callBack);
static void Main(string[] args)
{
UpdateDelegate myCallBack = new UpdateDelegate(Program.Report);
string path = @"C:Creative SolutionsProjectsClyde's Garden PlannerFrost Data DatabaseGSOD DataGSOD_RAW_DATA1992gsod_1992.txt";
int result = parse_raw_gsod_file(path, 32, myCallBack);
Console.Write("Parse completed with exit code: " + result.ToString());
Console.ReadLine();
} // end main function
public static void Report(string msg)
{
Console.Write("Message is ");
Console.WriteLine(msg);
}
} // End class
} // end namespace
I tried copying the DLL to the app output directory but it still can't find it. I also tried adding the DLL project as a reference but I get a popup window saying it can't be added. How do you properly link an unmanged DLL to a managed application?
Update - Here is the full error:
Unable to load DLL 'CGPUnmanagedLibrary': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Update 2 - I know for sure that the DLL is in the same directory as the .exe trying to load it. This makes me think there is a dependency in the DLL that isn't getting loaded. I'm only using basic C++ libraries in the DLL (string, math, iostream, etc). Any ideas what could not be loading and why?
Update 3 - Tested with Dependency Walker Loading my unmanaged C++ DLL in dependency walker showed no errors. I also tried to open my executable in dependency walker and it showed errors loading two DLLs: GPSVC.DLL and IESHIMS.DLL - doesn't make any sense because I am only using standard c++ libraries in my code. I think it may have something to do with the fact that I have a managed C++/CLI DLL trying to load the unmanaged DLL as well (I was trying to implement some C++/CLI wrappers). Anyway, I have since started a new VS solution and moved on. See my answer.
In all likelihood the problem isn't the DLL you're trying to load but one of its (chained) dependencies. Run depends.exe or a similar utility on the DLL to see if all the dependencies can be found. The misleading message "The specified module could not be found" has become a classic annoyance (if not FAQ material!): it leads you to think that your DLL is not being found when almost all of the time it's one of its dependencies that's not being found.
To test, the *.dll needs to be in the same directory as the .exe that is trying to load it. Don't trust Visual Studio to do it for you at this point. Physically copy the file to C:******Debugx86bin or whichever configuration you are running under. If in doubt, copy it to all of your bin folders. After you figure out the path, then you start finding ways to automate the project build to copy the file correctly. If that doesn't do it, put it in system32--it will certainly find it there. However, if after doing these things, you still can't find it. There is probably a dependency to your unmanaged dll that is also missing.
First I want to thank everyone for their help. Unfortunately I never did solve this issue (see edits in my main question). The answer turned out to be starting a totally new Visual Studio solution and creating two new projects: C# app and C++ dll. I did away with the need for wrappers as I am now just marshaling two main functions.
Thanks again.
链接地址: http://www.djcxy.com/p/44458.html