【问题标题】:Loading a 32-bit or 64-bit side-by-side COM DLL depending on the bitness with which the application runs根据应用程序运行的位数加载 32 位或 64 位并排 COM DLL
【发布时间】:2010-07-28 15:18:24
【问题描述】:

我有一个使用 COM DLL 的 .NET 应用程序,它有 32 位和 64 位版本。我编写了两个应用程序清单,它们使并行 COM 互操作可以在 32 位或 64 位上工作。这里是 32 位版本:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity name="MyApp" version="1.0.0.0" type="win32" />
  <dependency>
    <dependentAssembly>
      <assemblyIdentity 
           type="win32" 
           name="MyCOMDll_32.dll" 
           version="1.2.3.4" 
           processorArchitecture="x86" 
           publicKeyToken="0000000000000000"
           language="*" />
    </dependentAssembly>
  </dependency>
</assembly>

但是,维护两个清单会导致失去可移植性:您需要在安装应用程序时决定使用哪个版本。并且 64 位应用程序不能再在 32 位模式下运行。

是否有可能让 .NET 应用程序根据其运行的位数加载正确的 32 位或 64 位 DLL? 我尝试使用两个依赖元素,一个使用&lt;assemblyIdentity processorArchitecture="x86" .../&gt;,一个使用&lt;assemblyIdentity processorArchitecture="amd64" .../&gt;,但这会导致应用程序配置错误。

非常感谢您的回答。 问候, 莫里茨

【问题讨论】:

    标签: com-interop manifest side-by-side


    【解决方案1】:

    我还没有找到使用应用程序清单执行此操作的方法。因此,我放弃了应用程序清单以支持使用激活上下文 API 的编程解决方案。 此解决方案改编自 http://support.microsoft.com/kb/830033/en-us(其中字段 cookie 必须是 IntPtr 而不是 uint)。 我已将 EnsureActivationContextCreated() 方法的内部部分替换为

    
    if (!contextCreationSucceeded)
    {
        string manifestLoc = Environment.Is64BitProcess
        ? "MyCOMDll_64.dll.manifest"
        : "MyCOMDll_32.dll.manifest";
    
        myComActivationContext = new NativeMethods.ACTCTX();
        myComActivationContext.cbSize = Marshal.SizeOf(typeof(NativeMethods.ACTCTX));
        myComActivationContext.lpSource = manifestLoc;
    
        // Note this will fail gracefully if file specified
        // by manifestLoc doesn't exist.
        hActCtx = NativeMethods.CreateActCtx(ref myComActivationContext);
        contextCreationSucceeded = hActCtx != Failed;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 2013-03-02
      • 2020-07-28
      • 1970-01-01
      • 2018-05-29
      • 2012-05-22
      • 1970-01-01
      相关资源
      最近更新 更多