找不到Dll需要帮助(C#DLL) [英] Dll not found help is needed (C# DLL)

查看:91
本文介绍了找不到Dll需要帮助(C#DLL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这是我连续第二天撞到墙上的东西。



我是为第三方应用程序编写扩展。



申请表位于:

AppName\Bin\app.exe



应用程序可以被告知使用具有此名称的DLL,入口点具有此名称,而不是

它使用该DLL。此扩展程序dll应位于特定文件夹中:



AppName \Bin \Ext \



问题是,我的扩展DLL依赖于第二个DLL(也是第三方)。



我把两个dll都放在Ext子文件夹中:

AppName\Bin\Ext\A.dll

AppName\Bin\Ext\B.dll



两者dll是用C#编写的。 A.DLL是我写的,B.DLL是第三方dll。



当A.DLL只使用自己定义的对象/函数时,一切都很好。

当A.DLL使用B.DLL中的任何对象/函数时,会抛出FileNotFound异常。



现在疯狂的部分:如果我将B.DLL放在Bin文件夹或B文件夹中,一切正常:

AppName\Bin\B.dll - 工作

AppName \Bin \ B \ B.dll - 工作

AppName\Bin\Ext\B.dll - 无效。



我尝试过在使用它之前强制加载B.DLL(在A.DLL代码中) - 这没有帮助。 B.DLL已加载但未使用,并调用下一行使用它的对象尝试再次加载B.DLL,找不到它,并抛出异常。



可能是什么问题?为什么当A.DLL位于同一个文件夹中时,它不会寻找B.DLL ??



我尝试过的方法:



一切 - 到目前为止没有任何作用,不明白为什么在需要时找不到dll。

解决方案

< blockquote>好的,这里需要做的是强制dll在上述情况下在同一个forlder中查找引用的dll:



 public static class GlobalKeeper 
{
public static string assemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly()。CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
返回Path.GetDirectoryName(path);
}
}

public static Assembly assemblyResolveHandler(object sender,ResolveEventArgs args)
{
string assemblyPath = assemblyDirectory;
string [] words = args.Name.Split(',');
if(words.Length> 0)
assemblyPath + =\\+ words [0] +。dll;

返回Assembly.LoadFrom(assemblyPath);
}

public static void initialize()
{
AppDomain.CurrentDomain.AssemblyResolve + = assemblyResolveHandler;
}
}


Hi All,
here is the thing I'm hitting the wall with second day in a row.

I'm writing an extension to the 3rd party application.

Application resides in:
AppName\Bin\app.exe

Application can be told "use DLL with this name, entry point has this name", and than
it uses that dll. This extension dll should reside in a specific folder:

AppName\Bin\Ext\

Problem is, my extension DLL depends on a second DLL (also 3rd party).

I place both dlls reside in Ext subfolder:
AppName\Bin\Ext\A.dll
AppName\Bin\Ext\B.dll

Both dlls are written in C#. A.DLL is the one I wrote, B.DLL is 3rd party dll.

When A.DLL uses only objects/functions defined in itself, all is fine.
When A.DLL uses any object/function from B.DLL, FileNotFound exception is thrown.

And now crazy part: if I place B.DLL in Bin folder or "B" folder, everything works:
AppName\Bin\B.dll - works
AppName\Bin\B\B.dll - works
AppName\Bin\Ext\B.dll - does not work.

I tried to force loading B.DLL (in the A.DLL code) right before using objects from it - this did not help. B.DLL is loaded, but not used, and call on the next line to use object from it tries to load B.DLL again, not finding it, and throws exception.

What could be the problem? Why A.DLL is not looking for B.DLL when it is located in the same folder??!

What I have tried:

Everything - nothing works so far, don't understand why dll is not found when needed.

解决方案

Ok, here what needs to be done to force dll to look for referenced dll in the same forlder in the case described above:

public static class GlobalKeeper
{
   public static string assemblyDirectory
   {
      get
      {
         string codeBase = Assembly.GetExecutingAssembly().CodeBase;
         UriBuilder uri = new UriBuilder(codeBase);
         string path = Uri.UnescapeDataString(uri.Path);
         return Path.GetDirectoryName(path);
      }
   }

   public static Assembly assemblyResolveHandler(object sender, ResolveEventArgs args)
   {
      string assemblyPath = assemblyDirectory;
      string[] words = args.Name.Split(',');
      if (words.Length > 0)
         assemblyPath += "\\" + words[0] + ".dll";

      return Assembly.LoadFrom(assemblyPath);
   }

   public static void initialize()
   {
      AppDomain.CurrentDomain.AssemblyResolve += assemblyResolveHandler;
   }
}


这篇关于找不到Dll需要帮助(C#DLL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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