简体   繁体   English

将依赖的托管DLL添加到非托管C ++项目

[英]Add managed DLL dependencied to unmanaged C++ project

I have project with managed DLL A.dll which depends on managed B.dll and C.dll. 我有依赖托管B.dll和C.dll的托管DLL A.dll项目。

I expose A.DLL to unmanaged C++ project D via COM interface. 我通过COM接口将A.DLL公开给非托管C ++项目D。 Everything is okay... But A.DLL can't find D.dll and C.dll and raises appropriate exception. 一切正常...但是A.DLL找不到D.dll和C.dll并引发适当的异常。 I tried putting them in the same folder but it does not work. 我尝试将它们放在相同的文件夹中,但不起作用。 How and where should I reference those dependencies? 我应该如何以及在哪里引用这些依赖关系?

In C++ I would just build A.dll with static linking but .NET does not have this option. 在C ++中,我只用静态链接构建A.dll,但.NET没有此选项。

Update: putting library in the same directory as .exe file works, I just lost my binary. 更新:将库与.exe文件放在同一目录下,我只是丢失了二进制文件。

Normal CLR search rules apply here. 常规CLR搜索规则在此处适用。 It first looks in the GAC and next looks in the directory in which the EXE is located. 它首先在GAC中查找,然后在EXE所在的目录中查找。 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. 您可以说服COM运行时从注册中定位A.dll,它可以存储在Regasm.exe / codebase选项告诉它的外观的任何位置。 But that does not affect where the CLR looks for dependencies, it only considers the EXE location. 但是,这并不影响在CLR寻找依赖,只考虑EXE位置。

You can troubleshoot this by using the Fuslogvw.exe utility. 您可以使用Fuslogvw.exe实用程序解决此问题。

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. 只要您保证在A.dll中有一个[ComVisible]类型,可以确保首先实例化该类型(请考虑“ Application”),则可以在构造函数中订阅AppDomain.CurrentDomain.AssemblyResolve事件,以帮助CLR查找其他DLL。 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. 但是,构造函数不需要B或C类型非常重要,当抖动需要它们来编译构造函数时,您仍然会崩溃。

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. 如果那不是合适的选择,那么如果您希望将DLL部署在EXE安装目录的子目录中,则编写appname.exe.config文件可能会有所帮助。 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. 但是,在COM方案中,这很少是一个好主意,因为您通常无法控制EXE,这通常是其他人的责任。 Deploying locally is fine when you test your code. 测试代码时,本地部署很好。 For production deployment you ought to seriously consider the GAC. 对于生产部署,您应该认真考虑GAC。 In general a good idea in COM anyway since registration is machine-global which gives it strong DLL Hell headaches. 总之,无论如何,在COM中还是个好主意,因为注册是机器全局的,这使其产生了严重的DLL Hell头痛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM