注册用于32位和64位客户端的32位COM服务器(并生成TLB) [英] Register 32-bit COM server (and generate TLB) for use with 32-bit and 64-bit clients

查看:433
本文介绍了注册用于32位和64位客户端的32位COM服务器(并生成TLB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法1:x86目标平台

我有一个.NET类库,旨在与32位和64位客户端一起使用,其中有些是.NET项目,有些是C ++ MFC项目.我可以使该库与32位客户端一起使用,但是64位客户端会收到未注册类"异常.仔细查看注册表,似乎只设置了win32密钥(HKEY_CLASSES_ROOT \ Wow6432Node \ TypeLib {GUID} \ 1.0 \ 0 \ win32).

I have a .NET class library that is intended for use with both 32-bit and 64-bit clients, some of which are .NET projects and some of which are C++ MFC projects. I can get the library to work with the 32-bit clients, but the 64-bit clients get a "Class not registered" exception. Poking around the registry, it looks like only the win32 key is being set (HKEY_CLASSES_ROOT\Wow6432Node\TypeLib{GUID}\1.0\0\win32).

关于我的一些设置的一些注释:

Some notes on a few of my settings:

  • 选中注册COM互操作".
  • 选中使程序集在COM可见".
  • 平台目标"是x86.
  • 该库包含一个32位非托管DLL,可使用DLLImport加载方法.
  • C ++ MFC客户端使用#import "MyLibrary.tlb"编组互操作代码.
  • 我尚未对支持COM的代码进行任何更改. (例如,我尚未覆盖System.Configuration.Install.Installer.Install.)
  • "Register for COM interop" is checked.
  • "Make assembly COM-Visible" is checked.
  • The "Platform target" is x86.
  • The library contains a 32-bit unmanaged DLL that loads methods with DLLImport.
  • The C++ MFC clients use #import "MyLibrary.tlb" to marshall the interop code.
  • I have not made any changes to the code to support COM. (E.g., I have not overridden System.Configuration.Install.Installer.Install.)

为了解决这个问题,我根据一些研究和调查尝试了几件事.

In an effort to resolve this, I have tried several things based on some research and investigation.

方法2:任何CPU"目标平台

首先,我尝试将Platform目标设置为"Any CPU".在这种情况下,将在注册表中同时设置win32和win64项,并且64位客户端可以与COM服务器通信.但是,执行此操作时,出现错误试图加载格式错误的程序".这是由于32位非托管DLL所致,它要求我使用x86来构建项目.

First, I tried setting the Platform target to "Any CPU". In this case, both the win32 and win64 keys are set in the registry, and the 64-bit client can talk to the COM server. However, when I do this, I get the error "An attempt was made to load a program with the incorrect format." This is due to the 32-bit unmanaged DLL, which requires that I build the project with x86.

方法3:获取库的非托管DLL依赖项的64位版本

要解决此问题,我可能会保留非托管DLL的64位版本.但是,我们已经从第三方购买了该DLL,并且由于任何原因,他们会为DLL的64位版本收取大量的额外费用.除了显式成本之外,我还必须实现每个对象的DLLImport调用,并根据它是32位还是64位进行调用,这将使我的包装器类中的方法数量增加三倍.因此,尽管这是一种可能的解决方案,但它远非理想,而且据我了解,关于COM的理解也不是必须的.

To work around this, I could potentially get a hold of the 64-bit build of the unmanaged DLL. However, we have purchased this DLL from a third party, and for whatever reason, they charge a large, additional fee for the 64-bit build of the DLL. On top of the explicit cost, I would also have to implement DLLImport calls for each and call according to whether it's 32-bit or 64-bit, which would triple the number of methods in my wrapper class. Thus, while this is a possible solution, it's far from desirable, and from what I understand about COM, this shouldn't be necessary.

方法4:regsvr32.exe

为此,我在网上找到的最常见的解决方案是使用regsvr32.exe手动注册32位和64位互操作的应用程序.但是,我需要TLB文件才能与MFC客户端进行互操作,并且

The most common solution I find online for this is to use regsvr32.exe to manually register an application for both 32-bit and 64-bit interop. However, I need the TLB file for interoperability with my MFC clients, and apparently regsvr32 doesn't support this.

方法5:regasm.exe

要获取TLB文件并希望同时注册32位和64位互操作,我还尝试使用regasm.exe手动注册程序集.我使用以下命令来执行此操作.

To get the TLB file and hopefully register for both 32-bit and 64-bit interop, I have also tried using regasm.exe to register the assembly manually. I used the following command to do this.

"%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\regasm.exe" "D:\Path\To\MyLibrary.dll" /tlb /nologo

但是,此命令仅注册64位COM服务器,并且缺少win32 COM注册表项.现在,我的32位客户端找不到COM服务器,并说类未注册".此外,对于64位客户端,我遇到与选择任何CPU"时看到的相同错误:试图加载格式不正确的程序."

However, this registers only the 64-bit COM server and the win32 COM registry key is missing. Now my 32-bit client can't find the COM server and says "Class not registered." Furthermore, for the 64-bit client, I get the same error that I saw when I selected "Any CPU": "An attempt was made to load a program with an incorrect format."

TL; DR 如何注册用于32位和64位客户端应用程序的32位COM服务器? (请注意,我需要TLB文件,并且服务器需要加载非托管的32位DLL.)

TL;DR How do I register a 32-bit COM server for use with both 32-bit and 64-bit client applications? (Noting that I need the TLB file, and that the server needs to load an unmanaged, 32-bit DLL.)

我认为我应该在这里澄清架构.这是一个示意图,演示了我希望这些组件如何工作.

I think I should clarify the architecture here. Here's a diagram that demonstrates how I want these components to function.

如您所见,我需要64位代码才能通过COM服务器与32位代码进行交互,正如@ MC-ND所指出的那样,它应该可以工作.我应该如何配置我的COM服务器来支持它?

As you can see, I need 64-bit code to interact with 32-bit code by means of a COM server, which, as @MC-ND notes, is supposed to work. How should I configure my COM server to support this?

推荐答案

来自 MSDN:流程互操作性

在64位Windows上,64位进程无法加载32位动态链接 库(DLL).此外,32位进程无法加载64位DLL

On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link library (DLL). Additionally, a 32-bit process cannot load a 64-bit DLL

您最好的选择似乎是将您的组件编译为32位进程外COM服务器.

Your best option seems to compile your component as a 32bit out of process COM server.

这篇关于注册用于32位和64位客户端的32位COM服务器(并生成TLB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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