Gre*_*eld 17 .net c# clr 64-bit x86-64
我遇到的问题与此处描述的类似,但略有不同(加载程序集及其依赖项).
我有一个用于3D渲染的C++ DLL,这是我们向客户销售的产品.对于.NET用户,我们将有一个CLR包装器.C++ DLL可以在32位和64位版本中构建,但我认为这意味着我们需要有两个CLR包装器,因为CLR绑定到特定的DLL?
现在假设我们的客户有一个可以是32位或64位的.NET应用程序,并且它是一个纯粹的.NET应用程序,它让CLR从一组程序集中解决它.问题是应用程序代码如何在运行时动态选择我们的32位和64位CLR/DLL组合?
更具体地说,这里也适用于上述问题的建议答案(即创建一个ResolveEvent处理程序)?
我终于得到了一个似乎有效的答案.
将32位和64位版本(托管和非托管)编译到单独的文件夹中.然后让.NET应用程序在运行时选择从哪个目录加载程序集.
使用ResolveEvent的问题是,只有在找不到程序集时才会调用它,因此很容易意外地以32位版本结束.而是使用第二个AppDomain对象,我们可以将ApplicationBase属性更改为指向正确的文件夹.所以你最终得到的代码如下:
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);
}
Run Code Online (Sandbox Code Playgroud)
最终得到2个exes - 您的普通应用程序和第二个切换应用程序,用于选择要加载的位.注意 - 我自己也不能相信这个细节.我的一位同事怀疑我的初始指针.如果他注册StackOverflow,我会给他答案
归档时间: |
|
查看次数: |
4129 次 |
最近记录: |