加载C#DLL到C ++ / CLI - 依赖目录 [英] Loading C# DLL to C++/CLI - dependencies directory

查看:373
本文介绍了加载C#DLL到C ++ / CLI - 依赖目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个dllç它使用我的其他C#dll库++ / CLI库。当我在同一个文件夹中的应用程序,它调用了C#DLL C ++ / CLI库工作正常。该库将最后加载到许多应用程序和C#DLL不得复制与应用程序目录。它是在同一文件夹C ++ / CLI库,但在这种情况下,我已经得到了System.IO.FileNotFoundException。
我的建议是手动加载C#库或更改路径在哪里f.ex. Firefox是找的依赖性,但我试图与调用LoadLibrary()和大会:: LoadFrom()方法来强制加载从右目录。当然,我加入的dll到系统路径的目录路径。
我在VS2010工作。

I wrote a dll c++/cli library which uses my other c# dll library. C++/cli library works fine when I've got c# dll in the same folder as application which calls it. This library will be finally loaded to many applications and a C# dll must not be copied into directory with application. It has to be in the same folder as c++/cli library, but in that cases I've got System.IO.FileNotFoundException. My suggestion is to load c# library manually or to change path where f.ex. firefox is looking for dependencies, but I tried with LoadLibrary() and Assembly::LoadFrom() methods to force loading from right directory. Of course I added directory path with dll to system PATH. I work on VS2010.

推荐答案

您不要更改默认的目录中的应用程序将寻找的DLL。

You don't change the default directory where an application will look for dlls.

在设计时把你的DLL在某些众所周知的位置,你要部署到之一。添加一个引用到它,确保它被设置为永远不要复制,否则将在bin文件夹结束。你必须这样做,否则将无法编译。

At design time put your dll in some well know location, the one you are going to deploy to. Add a reference to it, make sure it's set to Don't copy ever, otherwise it will end up in the bin folder. You have to do this otherwise it won't compile.

当您部署,你需要一个软件包部署通用的DLL,并为每个应用程序。小心你不创建自己的DLL地狱的版本,如果需要的appA常见的DLL的旧的或新版本,相比APPB

When you deploy, you'll need one package to deploy common dlls, and one for each application. Careful you don't create your own version of dll hell, if appA needs an older or new version of the common dll, compared to AppB

添加AppDomain.AssemblyResolve事件主(适用于Windows应用程序)。在运行时,当您在您的DLL引用类型的程序集尚未加载的事件处理程序将被解雇,和。

Add an AppDomain.AssemblyResolve event to main (for windows app). At run time the event handler will get fired when you reference a type in your dll, and the assembly has not yet been loaded.

在那里你从井里加载已知位置。这通常是在配置,或在一个相对路径。

In there you load it from the well known location. That usually in config, or in a relative path.

E.g.
AllMyApps
  CommonDLLS
  MyFirstApp



所以路径您加载需要共同的DLL的将是..\CommonDlls\MyCommondll.dll。

So the path you load the required common dll from would be "..\CommonDlls\MyCommondll.dll".

NB你将要保护的DLL文件以某种方式,否则一个坏人也许能够注入他们的一个版本到您的应用程序,这将是坏...

NB you will want to secure the dlls in some way, otherwise a bad guy might be able to inject their version of one in to your app, which would be bad...

您可以使用此机制从远程服务器获取的DLL或数据库BLOB也是如此。

You can use this mechanism to get the dll from a remote server or a database blob as well.

最简单的例子只是几行代码,就看情况了。我花了大约15分钟,得到这个要在类似的情况。

The simplest case is only a few lines of code, just look the event up. Took me about 15 minutes to get this going in a similar scenario.

不是本机虽然,否则我会在代码粘贴上。

Not on this machine though, otherwise I'd have pasted in the code.

这篇关于加载C#DLL到C ++ / CLI - 依赖目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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