从 C# 调用托管 C# COM 对象 [英] Calling Managed C# COM Objects from C#

查看:30
本文介绍了从 C# 调用托管 C# COM 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 C# 调用 C# COM 对象.我在 C# 中创建了一个类库,并使用 tlbexe.exe 导出了一个类型库.然后我使用regtlibv12.exe 注册了类型库.但是,当我在 Visual Studio 中添加对我的 COM 对象的引用时,我收到一条错误消息:

I am trying to call a C# COM object from C#. I created a class library in C# and exported a Type Library using tlbexe.exe. I then registered the type library using regtlibv12.exe. However when I add a reference to my COM object in Visual Studio I get an error saying:

Active X 类型库 ... 是从 .NET 程序集导出的,无法添加为引用.改为添加对 .NET 程序集的引用."

"The Active X type library ... was exported from a .NET assembly and cannot be added as a reference. Add a reference to the .NET assembly instead."

任何帮助将不胜感激.

推荐答案

64位进程无法直接访问32位unmanage代码.64 位系统上的 COM 对象有 2 个域.一个用于 64 位进程,一个用于 32 位进程.它们在注册表中有不同的注册区域,因此如果您注册一个仅 32 位的 Com 对象,就 64 位进程而言是不存在的.现在,如果您有一个针对 ANY_CPU 的程序集,它可以由 CLR 加载到 64 位或 32 位进程中.但是,如果将 ANY_CPU 程序集加载到 64 位进程中,则仍然无法加载任何 32 位未编码的代码.artical 中提到的解决方案 使用 DCOM 和 IPC 创建从 64 位到 32 位代码的进程外调用.

A 64bit process cannot access the 32bit unmanage code directly. There are 2 domains for COM objects on a 64bit system. One for 64bit processes and one for 32bit processes. They have different registration areas in the registry, so if you regsiter a 32bit only Com object, as far as a 64bit process is concerned is does not exist. Now if you have an assembly that is target for ANY_CPU it can be loaded into either a 64bit or 32bit process by the CLR. However, if ANY_CPU assembly is loaded into a 64bit process, it still is not goign to be able to load any 32bit unmaged code. The solution metioned in the artical uses DCOM and IPC to create an out of process call from 64bit to 32bit code.

您可以使用 WCF 在 .Net 中更轻松地完成同样的事情.您创建访问您的 32 位托管代码的 32 位 WCF 服务器进程.您的 64 位进程是 WCF 客户端并调用 32 位服务器.您可以使用 WCF 支持的命名管道协议来避免网络堆栈.

You can do the same thing a lot easyer in .Net with WCF. You create a WCF server process that is 32bit that acesses your 32bit managed code. Your 64bit process is a WCF client and makes calls to the 32bit server. You can avoid the network stack by using the Named Pipes protical supported by WCF.

这篇关于从 C# 调用托管 C# COM 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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