在 C# 中创建 COM 自动化服务器 [英] Creating a COM Automation Server in C#

查看:16
本文介绍了在 C# 中创建 COM 自动化服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个用 C# 编写的 .NET 类库,它通过 COM 将其功能公开给 C++ 程序(.NET 之前).

I currently have a .NET class library written in C# that exposes its functionaility via COM to a C++ program (pre-.NET).

我们现在想要将库移出进程以释放主应用程序中的地址空间(它是一个图像处理应用程序,大图像占用地址空间).我记得我在 VB6 的日子里,可以创建一个OLE 自动化服务器".操作系统会在创建/销毁对象时自动启动和停止服务器 .exe.这看起来非常适合我们:据我所知,客户端不会有任何变化,除了它会使用 CLSCTX_LOCAL_SERVER 而不是 CLSCTX_INPROC_SERVER 调用 CoCreateInstance.

We now want to move the library out-of-process to free up address space in the main application (it is an image-processing application, and large images eat up address space). I remember from my VB6 days that one could create an "OLE automation server". The OS would automatically start and stop the server .exe as objects were created/destroyed. This looks like the perfect fit for us: as far as I can see nothing would change in the client except it would call CoCreateInstance with CLSCTX_LOCAL_SERVER instead of CLSCTX_INPROC_SERVER.

我将如何在 C# 中创建这样一个进程外服务器?要么网上没有相关信息,要么我的术语已经过时/过时了!

How would I create such an out-of-process server in C#? Either there is no information online about it, or my terminology is off/out of date!

推荐答案

您实际上可以在 .NET 中执行此操作(我之前已经作为概念验证做过),但是要获得所有内容需要一些工作工作权限(进程生命周期、注册等).

You can actually do this in .NET (I've done it before as a proof-of-concept), but it's a bit of work to get everything working right (process lifetime, registration, etc).

创建一个新的 Windows 应用程序.在 Main 方法中,调用 RegistrationServices.RegisterTypeForComClients - 这是一个围绕 CoRegisterClassObject 的托管包装器,它为您处理类工厂.将托管 ComVisible 类的类型(您实际想要创建的类 - .NET 自动提供类工厂)以及 RegistrationClassContext.LocalServer 和 RegistrationConnectionType.SingleUse 传递给它.现在您有一个非常基本的 exe,可以注册为 LocalServer32 以进行 COM 激活.您仍然需要计算进程的生命周期(使用构造函数/终结器在托管对象上实现引用计数——当你达到零时,调用 UnregisterTypeForComClients 并退出)——你不能让 Main 退出,直到你的所有对象都死了.

Create a new Windows application. In the Main method, call RegistrationServices.RegisterTypeForComClients- this is a managed wrapper around CoRegisterClassObject that takes care of the class factory for you. Pass it the Type of the managed ComVisible class (the one you actually want to create- .NET supplies the class factory automatically) along with RegistrationClassContext.LocalServer and RegistrationConnectionType.SingleUse. Now you have a very basic exe that can be registered as a LocalServer32 for COM activation. You'll still have to work out the lifetime of the process (implement refcounts on the managed objects with constructors/finalizers- when you hit zero, call UnregisterTypeForComClients and exit)- you can't let Main exit until all your objects are dead.

注册还不错:创建一个 ComRegisterFunction 属性方法,在 HKLMCLSID(yourclsidhere) 下添加一个 LocalServer32 键,其默认值是您的 exe 的路径.运行 regasm yourexe.exe/codebase/tlb,你就可以开始了.

The registration isn't too bad: create a ComRegisterFunction attributed method that adds a LocalServer32 key under HKLMCLSID(yourclsidhere), whose default value is the path to your exe. Run regasm yourexe.exe /codebase /tlb, and you're good to go.

这篇关于在 C# 中创建 COM 自动化服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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