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

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

问题描述

我现在有一个.NET类库C#编写的,它通过COM公开其functionaility的C ++程序(pre-.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作为创建对象/销毁服务器。这看起来像是一个完美的适合我们:。据我什么也看不到,在客户端会改变,除非它会调用CoCreateInstance与CLSCTX_LOCAL_SERVER而不是CLSCTX_INPROC_SERVER

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特性的类(你真的想create- .NET自动提供类工厂的)以及RegistrationClassContext.LocalServer和RegistrationConnectionType.SingleUse的类型。现在你有一个可以被注册为LocalServer32的COM激活一个非常基本的exe文件。你还必须制定出流程的生命周期(引用计数实现与构造函数/ finalizers-被管理的对象,当你打零,呼叫UnregisterTypeForComClients和出口) - 你不能让主退出,直到所有的对象都死了。

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归因方法,增加了在HKLM \ CLSID(yourclsidhere),其默认值为的路径EXE一个LocalServer32项。运行regasm yourexe.exe / codeBase类/ TLB,你是好去。

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

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

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