添加托管DLL依赖于非托管C ++项目 [英] Add managed DLL dependencied to unmanaged C++ project

查看:217
本文介绍了添加托管DLL依赖于非托管C ++项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有项目管理DLL A.dll这取决于管理的B.dll和C.dll。

I have project with managed DLL A.dll which depends on managed B.dll and C.dll.

我公开A.DLL到非托管C ++项目D通过COM接口。一切都好吧,但是A.DLL找不到D.dll和C.dll并引发适当的异常。我试着把它们放在同一个文件夹,但它不工作。

I expose A.DLL to unmanaged C++ project D via COM interface. Everything is okay... But A.DLL can't find D.dll and C.dll and raises appropriate exception. I tried putting them in the same folder but it does not work. How and where should I reference those dependencies?

在C ++中,我只是用静态链接构建A.dll,但是.NET没有这个选项。

In C++ I would just build A.dll with static linking but .NET does not have this option.

更新:将库放在与.exe文件相同的目录下,我刚丢失了我的二进制文件。

Update: putting library in the same directory as .exe file works, I just lost my binary.

推荐答案

正常CLR搜索规则适用于此处。它首先在GAC中查找,然后在EXE所在的目录中查找。你可以说服COM运行时从注册中找到A.dll,它可以存储在Regasm.exe / codebase选项告诉它的任何地方。但是会影响CLR查找依赖关系的位置,它只考虑EXE位置。

Normal CLR search rules apply here. It first looks in the GAC and next looks in the directory in which the EXE is located. You can convince the COM runtime to locate A.dll from the registration, it can be stored anywhere the Regasm.exe /codebase option tells it to look. But that does not affect where the CLR looks for dependencies, it only considers the EXE location.

您可以使用Fuslogvw进行故障排除。 exe实用程序。

You can troubleshoot this by using the Fuslogvw.exe utility.

替代方法通常很麻烦。只要您在A.dll中有一个[ComVisible]类型,保证首先实例化(认为应用程序),那么您可以在构造函数中订阅AppDomain.CurrentDomain.AssemblyResolve事件,以帮助CLR定位其他DLL。但是非常重要的是构造函数不需要B或C的类型,当抖动需要他们编译构造函数时,你仍然会崩溃。

Alternatives are in general troublesome. As long as you have a [ComVisible] type in A.dll that's guaranteed to be instantiated first (think "Application") then you can subscribe the AppDomain.CurrentDomain.AssemblyResolve event in the constructor to help the CLR locate the other DLLs. But it is very important the constructor doesn't need types from B or C, you'll still crash when the jitter needs them to compile the constructor.

如果不是一个合适的选项然后写一个appname.exe.config文件可以有点有用,如果你喜欢部署在EXE安装目录的子目录中的DLL。这在COM场景中很少是一个好主意,因为你通常不能控制EXE,它通常是别人的责任。当您测试代码时,在本地部署是很好的。对于生产部署,您应该认真考虑GAC。一般来说一个好主意在COM无论如何,因为注册是机器全局,给它强大的DLL地狱头痛。

If that's not a suitable option then writing an appname.exe.config file can be somewhat useful if you prefer deploying the DLLs in a subdirectory of the EXE install directory. This is however rarely a good idea in a COM scenario since you are typically not in control over the EXE, it is usually somebody else's responsibility. Deploying locally is fine when you test your code. For production deployment you ought to seriously consider the GAC. In general a good idea in COM anyway since registration is machine-global which gives it strong DLL Hell headaches.

这篇关于添加托管DLL依赖于非托管C ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆