如何后期绑定的32位/ 64位库在运行时 [英] How to late bind 32bit/64 bit libs at runtime

查看:112
本文介绍了如何后期绑定的32位/ 64位库在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相似,但略有不同的问题,这说明这里(装载程序集和它们的相关性)。

I've got a problem similar to,but subtly different from, that described here (Loading assemblies and their dependencies).

我有一个C ++ DLL的3D渲染这是我们销售给客户。对于.NET的用户,我们将有它周围的CLR包装。 C ++的DLL可以建立在32位和64位版本,但我认为这意味着我们需要有两个CLR包装,因为CLR绑定到特定的DLL?

I have a C++ DLL for 3D rendering that is what we sell to customers. For .NET users we will have a CLR wrapper around it. The C++ DLL can be built in both 32 and 64bit versions, but I think this means we need to have two CLR wrappers since the CLR binds to a specific DLL?

说,现在我们的客户有一个.NET应用程序,可以是32位或64位,而且它是一个纯粹的.NET应用程序,它离开CLR从单一的一套组件做得出来。现在的问题是如何应用程序code我们的32位和64位CLR / DLL组合之间在运行时动态选择?

Say now our customer has a .NET app that can be either 32 or 64bit, and that it being a pure .NET app it leaves the CLR to work it out from a single set of assemblies. The question is how can the app code dynamically choose between our 32 and 64bit CLR/DLL combinations at run-time?

甚至更具体地,是所建议的答案适用这里太上述问题(即,创建一个处理程序ResolveEvent)?

Even more specifically, is the suggested answer to the aforementioned question applicable here too (i.e. create a ResolveEvent handler)?

推荐答案

我终于有了这个,似乎工作的一个答案。

I finally have an answer for this that appears to work.

编译两个32安培; 64位版本 - 包括管理及放大器;非托管 - 到单独的文件夹中。然后在.NET应用程序选择在运行时该目录从加载的程序集。

Compile both 32 & 64 bit versions - both managed & unmanaged - into separate folders. Then have the .NET app choose at run time which directory to load the assemblies from.

与使用ResolveEvent的问题是,它仅被调用如果未找到组件,所以它是所有以容易意外结束与32位版本。而是使用第二AppDomain中的对象,我们可以在正确的文件夹中更改ApplicationBase属性点。所以,你最终与code这样的:

The problem with using the ResolveEvent is that it only gets called if assemblies aren't found, so it is all to easy to accidentally end up with 32 bit versions. Instead use a second AppDomain object where we can change the ApplicationBase property to point at the right folder. So you end up with code like:

static void Main(String[] argv)
  {
     // Create a new AppDomain, but with the base directory set to either the 32-bit or 64-bit
     // sub-directories.

     AppDomainSetup objADS = new AppDomainSetup();

     System.String assemblyDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
     switch (System.IntPtr.Size)
     {
        case (4): assemblyDir += "\\win32\\";
           break;
        case (8): assemblyDir += "\\x64\\";
           break;
     }

     objADS.ApplicationBase = assemblyDir;

     // We set the PrivateBinPath to the application directory, so that we can still
     // load the platform neutral assemblies from the app directory.
     objADS.PrivateBinPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

     AppDomain objAD = AppDomain.CreateDomain("", null, objADS);
     if (argv.Length > 0)
        objAD.ExecuteAssembly(argv[0]);
     else
        objAD.ExecuteAssembly("MyApplication.exe");

     AppDomain.Unload(objAD);

  }

您结束了2前男友 - 你的正常的应用程序和第二开关的应用程序,选择哪些位加载。 注 - 我不能邀功的这个自己的细节。我的一个同事sussed说出来给我最初的指针。如果当他签约到计算器,我会分配给他的答复

You end up with 2 exes - your normal app and a second switching app that chooses which bits to load. Note - I can't take credit for the details of this myself. One of my colleagues sussed that out given my initial pointer. If and when he signs up to StackOverflow I'll assign the answer to him

这篇关于如何后期绑定的32位/ 64位库在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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