0

I am hitting the exception:

Unable to load DLL 'MyDll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

As the answer to Unable to load DLL (Module could not be found HRESULT: 0x8007007E) suggests, we want to keep the unmanaged dll in the current directory, together with all the managed dlls (and it is there). How can I, for debugging purposes, print the current directory that will be used to load the dll? Is it Directory.GetCurrentDirectory()?

Community
  • 1
  • 1
Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • Dll folder search order is described here: http://stackoverflow.com/questions/268627/where-does-dllimport-look-for-unmanaged-dlls You can also explicitly set search directory like described here: http://stackoverflow.com/questions/2864673/specify-the-search-path-for-dllimport-in-net – sasha_gud Jul 22 '14 at 16:11
  • The current directory is not particularly relevant. You aren't showing any code or giving us enough details. – David Heffernan Jul 22 '14 at 19:21
  • @DavidHeffernan, how come the current directory is not relevant? It should be second place to look for the dll... – Grzenio Jul 23 '14 at 09:23
  • If you are relying on the current directory to locate your DLL, you are doing it all wrong. Put the unmanaged DLL in the same directory as the executable and be done with it. – David Heffernan Jul 23 '14 at 09:34
  • @DavidHeffernan, this is what we do for production, but for integration testing we use NUnit, and the test runner is obviously in a different location. – Grzenio Jul 23 '14 at 09:37
  • How are you loading the unmanaged DLL? FWIW, the top voted answer at the question you link to is factually incorrect. Here is the correct information: http://msdn.microsoft.com/en-us/library/ms682586.aspx – David Heffernan Jul 23 '14 at 09:38
  • @DavidHeffernan, Just calling a method decorated with `PInvoke` (with just a name of the dll). It works fine on my machine, one of the test suits works fine on my colleague's machine, but the other one fails. – Grzenio Jul 23 '14 at 14:06
  • 1
    If you need to control where the DLL is loaded from, and you cannot put the DLL in the executable folder, I find it easiest to use an explicit call to `LoadLibrary` passing the full path to the DLL. Or alternatively call `SetDllDirectory` or `AddDllDirectory`. I prefer `LoadLibrary` personally. – David Heffernan Jul 23 '14 at 14:09
  • @DavidHeffernan, seems like a perfect option for tests, cheers. – Grzenio Jul 23 '14 at 16:26

1 Answers1

0

You can use Environment.CurrentDirectory to get the current working directory for your application. This should give you want you want.

Assembly.GetExecutingAssembly().Location will return the directory that contains the executable or DLL that's currently executing.

CurrentDirectory can be changed by FileDialogs and other classes. So, it may not be the same as the directory that contains the entry point for the application.

Pat Hensel
  • 1,274
  • 9
  • 11