如何使用 C# 重新实现旧的 DCOM 服务器而不破坏现有客户端?

发布于 2024-07-25 19:45:50 字数 1204 浏览 6 评论 0原文

以前,我使用 ATL 库使用 VC++ 7.x(Visual Studio 2003 和 .Net 1.1)编写了一个进程外(即 EXE)COM 服务器。 服务器使用多线程单元 (MTA) 模型,并由 DCOM 调用(VB6 客户端)激活。 多个客户端请求由单个进程满足——该进程由第一个请求激活(由 COM 基础结构自动激活)。

这是一段相当复杂的代码,我现在必须对其进行一些更改。 不幸的是,我构建它的机器早已不复存在,甚至在使用 VS 2003 的新机器上编译该项目时也遇到了问题。几年前,我曾考虑过将这个项目移至 VS 2005,但有很多问题对 ATL 库进行了重大更改,我放弃了这项努力。

我没有解决当前构建这个旧的 VS 2003 C++ 项目时遇到的问题,而是尝试在 .NET 中重写该项目。 这将使我的小商店(我们不再使用 C++)更易于维护。 ATL 宏和 C++ 习惯用法早已从我的记忆中消失了,我想将它们保留在那里。 :-)

所以我正在探索在 C#/VS2008 中进行重写的可行性。 我知道您可以向 COM 客户端公开 .NET 类。 事实上,我过去已经用一些简单的东西做到了这一点。 然而,这要复杂得多。 我不确定很多事情:

  1. 接口都是在客户端使用的类型库中定义的(必须保持不变)。 我可以假设我可以构建一个基于现有类型库实现这些接口的 .NET 服务器吗?

  2. 服务器实现了许多继承自IDispatch并标有“dual”和“oleautomation”的接口。 举个例子:

    [odl、uid(...)、双、oleautomation] 
      接口 IKDFTSearchManager : IDispatch { 
    
              HRESULT 方法 1(...); 
              HResult 方法2(...); 
      } 
      

    我不相信任何客户端使用 IDispatch 方法,但我假设互操​​作生成的 VTable 必须匹配。 如何从 .NET 服务器公开这一点?

  3. 什么类型的项目应该容纳这些组件? 控制台应用程序?

  4. 如果使用互操作工具之一正确注册,DCOM 可能会激活 EXE。 是这样吗? 基础设施的行为是否与 C++/ATL 服务器相同——即导致激活单个 EXE 来服务多个客户端请求?

  5. 与 (4) 相关,此服务器是否会使用多线程单元模型?

Back in the day, I wrote an out-of-process (i.e. EXE) COM server using VC++ 7.x (Visual Studio 2003 and .Net 1.1) using the ATL library. The server used the Multi Threaded Apartment (MTA) model and is activated by DCOM calls (VB6 clients). Multiple client requests are satisfied by a single process -- which is activated by the first request (automatically by the COM infrastructure).

It was quite a complex piece of code and I now have to make some changes to it. Unfortunately, the machine I built it on is long gone and I'm having problems even compiling the project on a new machine with VS 2003. A few years ago, I toyed with moving this project to VS 2005, but there were a lot of breaking changes to the ATL library and I gave up on that effort.

Rather than tackle with my current problems building this old VS 2003 C++ project, I'm toying with rewriting the thing in .NET. This will make it much more maintainable at my small shop (where we don't do C++ anymore). The ATL macros and C++ idioms are long since gone from my memory, and I'd like to keep them there. :-)

So I'm exploring how feasible it is to do this rewite in C#/VS2008. I know you can expose .NET classes to COM clients. Indeed I've done so with some simple stuff in the past. However, this is much more complex. I'm not sure of a number of things:

  1. The interfaces are all defined in a type library that is consumed by the clients (that must remain unmodifed). May I assume that I can build a .NET server that implements these interfaces based on an existing type library?

  2. The server implements a number of interfaces that inherit from IDispatch and are marked with "dual" and "oleautomation". As an example:

    [odl, uid(...), dual, oleautomation]
    interface IKDFTSearchManager : IDispatch {
    
            HRESULT Method1(...);
            HResult Method2(...);
    }
    

    I don't believe that any of the clients use the IDispatch methods, but I would assume that the VTable generated by interop must match up. How would one expose this from a .NET server?

  3. What type of project should house the components? A console application?

  4. DCOM would presumably activate the EXE if it is appropriately registered with one of the interop tools. Is that the case? Would the infrastructure behave as it did with the C++/ATL server -- i.e. cause a single EXE to be activated that serviced multiple client requests?

  5. Related to (4), would this server be using the Multi Threaded Apartment model?

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

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

发布评论

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

评论(2

落花随流水 2024-08-01 19:45:50

首先,我建议阅读 这个 (曾经用 C# 彻底重写过大型 C++ 应用程序吗?)

您应该查看 ServicedComponent 这是在 .NET 中实现 DCOM 服务器的官方方法。

该项目将是一个由 COM+ 托管的类库,您可以将项目作为服务托管,也不需要编写托管 COM+ 会处理的事情。

您可以引用旧的 tlb(但我建议在项目中编写 p/invoke 接口)并实现接口。

您需要使用 ProgIdAttribute 来保留旧的。

To begin with I would recommend reading this (Ever done a total rewrite of a large C++ application in C#?)

You should look into ServicedComponent which is the official way of implementing a DCOM server in .NET.

The project will be a class library that will hosted by the COM+ you can host the project as service also again you don't need to write the host COM+ will take care for that.

You can reference you old tlb (but I would recommend writing p/invoke interface inside your project) and implement the interfaces.

You will need to use ProgIdAttribute to keep the old one.

花想c 2024-08-01 19:45:50

我来自类似的背景,我可以想象将会付出巨大的努力来做到这一点,特别是如果你根本不想接触客户的话。

有一个名为 Tlbexp.exe 它将在 .Net 程序集中为您导出类型库。 我假设您需要使用此工具针对新的 C# 程序集 (dll) 运行,以生成与旧 COM 服务器相同的类型库。

I come from the similar background, and I can image there will be significant efforts to do this especially if you don't want to touch the client at all.

There is .Net sdk tool called Tlbexp.exe which will export the type library for you on .Net assembly. I assume you need use this tool to run against your new c# assembly (dll) to generate the same type library as your old COM server.

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