根据应用程序运行的位数加载 32 位或 64 位并行 COM DLL

发布于 2024-09-10 23:10:36 字数 1020 浏览 10 评论 0原文

我有一个使用 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? 我尝试使用两个依赖项元素,一个使用 < assemblyIdentity processArchitecture="x86" .../> ,另一个使用 < assemblyIdentity processArchitecture="amd64" .../> ;,但这会导致应用程序配置错误。

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

I have a .NET application that uses a COM DLL, of which there is both a 32bit and a 64bit version. I have written two application manifests that make side-by-side COM interop work on either 32 Bit or 64 Bit. Here the 32-bit version:

<?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>

However, maintaining two manifests leads to the loss of portability: you need to decide which version to use when you install the application. And the 64-bit application can no longer be run in 32-bit mode.

Is there a possibility to get the .NET application to load the correct 32-bit or 64-bit DLL depending on the bitness under which it runs?
I have tried using two dependency elements, one with <assemblyIdentity processorArchitecture="x86" .../> and one with <assemblyIdentity processorArchitecture="amd64" .../>, but that results in an application configuration error.

I'd be very grateful for answers.
Regards,
Moritz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2024-09-17 23:10:36

我还没有找到使用应用程序清单来执行此操作的方法。因此,我放弃了应用程序清单,转而采用使用激活上下文 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;
}

I have not found a way to do this with an application manifest.Therefore, I dropped the application manifest in favor of a programmatic solution using the Activation context API.
This solution has been adapted from http://support.microsoft.com/kb/830033/en-us (where the field cookie must be an IntPtr not a uint).
I have replaced the inner part of the EnsureActivationContextCreated() method by


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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文