为什么我的应用程序无法在同一目录中找到依赖的 dll?
我有一个简单的控制台测试应用程序 ConsoleApplication1.exe
,它引用另一个程序集 clipper.dll
。
在我测试过的 3 台机器上,可以将这两个文件放在例如 c:\test\
中,并执行 ConsoleApplication1.exe
。
在另一台机器(恰好是客户端机器)上,运行 ConsoleApplication1.exe
会导致程序崩溃,并且以下内容会打印到控制台:
C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 7C46-414F
Directory of C:\test
07/12/2010 06:08 PM <DIR> .
07/12/2010 06:08 PM <DIR> ..
07/12/2010 05:13 PM 11,776 ClassLibrary1.dll
07/12/2010 05:13 PM 30,208 ClassLibrary1.pdb
07/12/2010 04:55 PM 3,572 ClassLibrary1.tlb
19/11/2010 02:46 PM 235,008 clipper.dll
19/11/2010 02:46 PM 1,534,976 clipper.pdb
07/12/2010 05:13 PM 6,144 ConsoleApplication1.exe
07/12/2010 05:13 PM 11,776 ConsoleApplication1.pdb
01/08/2010 12:52 PM 139,264 nunit.core.dll
01/08/2010 06:41 AM 57,344 nunit.core.interfaces.dll
01/08/2010 06:41 AM 135,168 nunit.framework.dll
01/08/2010 06:41 AM 547,262 nunit.framework.xml
11 File(s) 2,712,498 bytes
2 Dir(s) 477,821,784,064 bytes free
C:\test>ConsoleApplication1.exe
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass
embly 'clipper.dll' or one of its dependencies. The specified module could not b
e found.
at ConsoleApplication1.Program.Main(String[] args)
这对我来说很奇怪,尽管我不知道全面了解dll加载规则我以为它会在CWD中搜索指定文件。事实上,同样的设置可以在我尝试过的其他各种计算机上运行,这也很奇怪。
奇怪的是,在工作计算机上,如果我删除 Clipper.dll 然后运行它:
C:\Temp>ConsoleApplication1.exe
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass
embly 'clipper, Version=1.0.3975.26584, Culture=neutral, PublicKeyToken=null' or
one of its dependencies. The system cannot find the file specified.
at ConsoleApplication1.Program.Main(String[] args)
未处理的异常略有不同,具有完整的程序集名称。
ConsoleApplication1.exe
和clipper.dll
都是为 .NET 4 构建的。- 所有计算机都安装了 .NET 4 运行时。问题机器没有 .NET sdk
- Clipper.dll 是非托管 C++ 代码和 C++/CLI 的组合。这是一个可疑的字符,因为它基本上是我创建的第一个 C++/CLI .NET 程序集,并且涉及很多摸索。它似乎可以很好地加载在反射器中。
- 当我试图找出为什么我无法成功利用 COM 对象(称之为 foo.dll )时,我做出了这些努力,该对象也引用了 Clipper.dll - 所有也在同一台有问题的机器上。经过几个小时的高潮地狱后,我能够进一步将其隔离到此。
I have a simple console test application, ConsoleApplication1.exe
, which references another assembly clipper.dll
.
On 3 machines that I've tested it on, it is possible to place both files in e.g. c:\test\
, and execute ConsoleApplication1.exe
.
On one other machine, which happens to be a client machine, running ConsoleApplication1.exe
results in a program crash and the following being printed to the console:
C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 7C46-414F
Directory of C:\test
07/12/2010 06:08 PM <DIR> .
07/12/2010 06:08 PM <DIR> ..
07/12/2010 05:13 PM 11,776 ClassLibrary1.dll
07/12/2010 05:13 PM 30,208 ClassLibrary1.pdb
07/12/2010 04:55 PM 3,572 ClassLibrary1.tlb
19/11/2010 02:46 PM 235,008 clipper.dll
19/11/2010 02:46 PM 1,534,976 clipper.pdb
07/12/2010 05:13 PM 6,144 ConsoleApplication1.exe
07/12/2010 05:13 PM 11,776 ConsoleApplication1.pdb
01/08/2010 12:52 PM 139,264 nunit.core.dll
01/08/2010 06:41 AM 57,344 nunit.core.interfaces.dll
01/08/2010 06:41 AM 135,168 nunit.framework.dll
01/08/2010 06:41 AM 547,262 nunit.framework.xml
11 File(s) 2,712,498 bytes
2 Dir(s) 477,821,784,064 bytes free
C:\test>ConsoleApplication1.exe
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass
embly 'clipper.dll' or one of its dependencies. The specified module could not b
e found.
at ConsoleApplication1.Program.Main(String[] args)
This is a odd to me, although I don't understand dll load rules comprehensively I thought it would search the CWD for the specified file. And the fact this same setup works on the various other computers I've tried is also odd.
Curiously, on a working computer if I remove clipper.dll and then run it:
C:\Temp>ConsoleApplication1.exe
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass
embly 'clipper, Version=1.0.3975.26584, Culture=neutral, PublicKeyToken=null' or
one of its dependencies. The system cannot find the file specified.
at ConsoleApplication1.Program.Main(String[] args)
The unhandled exception is slightly different, with a full assembly name.
- Both
ConsoleApplication1.exe
andclipper.dll
are built for .NET 4. - All machines have the .NET 4 runtime installed. The problem machine does not have the .NET sdk
- clipper.dll is a combination of ummanaged c++ code and C++/CLI. It is a suspicious character because it is basically the first C++/CLI .NET assembly I have created and much fumbling was involved. It seems to load in reflector fine.
- These efforts came about when I was trying to figure out why I was unable to successfully utilise a COM object (call it
foo.dll
) which also referencedclipper.dll
- all also on the same problem machine. After several hours of regasm hell I was able to further isolate it to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否部署了 CRT 运行时 DLL?确保部署程序集的发布版本。 CRT 的调试版本不可重新分发。
Did you deploy the CRT runtime DLLs on that machine? Make sure to deploy the Release build of your assembly. The debug version of the CRT is not redistributable.
我不知道为什么它发生在你身上,但如果你将 dll 复制到 system32 目录,它将导致 2 个选项
1)它会起作用
2)它不会工作,这样你就会知道它与dll路径无关
I don't know why it happed to u, but if you will copy the dll to system32 directory it will lead to 2 option
1) It will work
2) It won't work and that way you will know it doesn't relate to the dll path
确保包含 C++ 运行时库 (CRT< /a>) 在您的客户端计算机上。
Make sure to include the C++ runtime Libraries (CRT) on your client machine.
嗯,可能有很多原因
COM 对象 (foo.dll) 是否在有问题的计算机上注册?
请同时从安装文件夹中删除 pdbs。
Well , there may be lot of reasons
is the COM object (foo.dll) registered on the troubled computer?
Please remove pdbs from the installation folder also.
您的应用程序文件是从互联网下载的吗?我曾经遇到过同样的问题,我的程序被下载为 zip 文件,然后在客户端计算机上解压。提取后,我收到完全相同的错误消息“指定的模块无法b
原来
是Windows针对从互联网下载的文件的安全策略。Windows会“阻止”下载的文件,需要手动解锁。就我而言,我需要右键单击该文件,选择属性然后选择“unblock”,我的应用程序就可以正常加载程序集
http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=mpe&DownloadId=163767
或者,您可以在解压之前解锁 zip 文件,然后全部解压文件也将被解锁。
Are your application files downloaded from the internet? I once had the same problem where my program was downloaded in a zip file, and then extracted on the client machine. Once extracted I was getting exactly the same error message "The specified module could not b
e found."
It turned out to be Windows security policy for files downloaded from the internet. Windows will "block" downloaded files, and they need to be unblocked manually. In my case, I needed to right click on the file, select properties and then select "unblock", my application was then able to load the assembly as normal.
http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=mpe&DownloadId=163767
Alternatively you can unblock the zip file before extracting it, and then all extracted files will also be unblocked.