dllimport failed to locate dll even though it is in the PATH
Asked Answered
A

4

8

I use [Dllimport("DllName.dll")] where I'm sure a path to my dll exists in the process PATH environment variable, and still I get "DllName.dll could not be found"

Agace answered 16/11, 2008 at 16:43 Comment(1)
Please Check Target Platform( X86 or x64 or AnyCPU)Aesthetics
S
14

"DllName.dll could not be found" could also mean that DllImport has not found one of DllName.dll dependencies.

Grab Dependecy Walker to check which dependecy you are missing.

Strobila answered 17/3, 2009 at 12:56 Comment(0)
F
1

I'd suggest you to use FileMon (sysinternals.com) to check if your assumptions about the library location are right.

Flagella answered 16/11, 2008 at 17:5 Comment(0)
C
1

DllImport does not consult the PATH environment variable when loading DLL's. I am fairly certain that DllImport follows the standard rules for loading a native DLL in a native program when the DLL is referenced by name. Here is an article which details those rules

http://msdn.microsoft.com/en-us/library/ms682586.aspx

I've found the simplest way to load the DLL is just to put it in the same location as the program I am executing.

Carpi answered 16/11, 2008 at 17:52 Comment(2)
Maybe I'm miss understanding your answer but it doesn't sound right. I created a simple asp.net web app that uses the DllImport attribute to call a simple exported function from a custom c++ dll. Without updating the path environment variable I get the "Unable to load DLL" exception. However, when the path environment variable is updated to include the path to dll I can call the external function just fine.Balthazar
I'm having the same problem here. if i use DllImport("sample.dll"), it wont work. But if I try DllImport("C:\\Somelocation\\sample.dll"), it works.Timbering
C
1

I used const string lcms2Path = "C:\\lcms2.dll"; in my C# application for the longest time. But finally moved the DLL to the BIN folder and this works perfect [DllImport("lcms2.dll")].

Chauvinism answered 10/2, 2017 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.