Using DllImport to load an unmanaged dll into a managed application

In my project, I have an unmanaged native C ++ dll and a C # application. I am trying to import a function from an unmanaged dll using DllImport, but I keep getting 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 Solutions\Projects\Clyde Garden Planner\Frost Data Database\GSOD Data\GSOD_RAW_DATA\1992\gsod_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 to copy the DLL to the application's output directory, but it still can't find it. I also tried to add the DLL project as a reference, but I get a popup that says it cannot be added. How to properly link an unused DLL to a managed application?

Refresh . Here is the complete 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 won't load. I only use basic C ++ libraries in DLLs (string, math, iostream, etc.). Any ideas which can't be downloaded and why?

Update 3 - Tested with Walker Dependency Walker Loading my unmanaged C ++ DLL in dependency run showed no errors. I also tried to open my executable in dependent walker and it showed two DLL load errors: GPSVC.DLL and IESHIMS.DLL - doesn't make any sense because I only use standard C ++ libraries in my code. I think it might be because I have a managed C ++ / CLI DLL trying to load an unmanaged DLL (I was trying to implement some C ++ / CLI wrappers). Anyway, since then I started a new VS solution and moved on. See my answer.

+3


source to share


4 answers


First, I want to thank everyone for their help. Sadly, I never really got it right (see the edits in my main question). The answer turned out to be the beginning of a completely new Visual Studio solution and creating two new projects: C # app and C ++ dll. I have done away with the need for a wrapper since now I am just separating the two main functions.



Thanks again.

0


source


Apparently, the problem is not with the DLL you are trying to load, but with one of its (chained) dependencies. Run depends.exe or a similar DLL utility to see if all dependencies can be found. The error message "The specified module could not be found" has become a classic annoyance (if not frequently asked stuff!): It makes you think your DLL was not found when almost all the time it is one of its dependencies, not.



+4


source


To test, the * .dll file must be in the same directory as the .exe that is trying to load it. Don't trust Visual Studio to do this for you at this point. Physically copy the file to C: ****** \ Debug \ x86 \ bin \ or whatever configuration you are running in. If in doubt, copy it to all bin folders. Once you figure out the path, you start looking for ways to automate the build of the project so that you copy the file correctly. If it doesn't, put it in system32 - it will definitely find it there. However, if after that you still cannot find it. There is probably a dependency on your unmanaged DLL that is also missing.

+2


source


User arayq2 made a lot of sense and I was able to solve my problem quickly too.

The DLL that cannot be loaded (DllNotFoundException) in my case depends on another dll. This DLL (not part of my project) was actually compiled with newer versions of certain .h and .lib files. Older versions of these .h and .lib files (with the same name) were part of a project that compiled a DLL that could not be loaded.

After I updated the dll project with newer versions of these .h and .lib files and recompiled the dll project my problem was resolved.

Thanks arayq2!

0


source







All Articles