简体   繁体   English

通过C#访问远程COM对象

[英]Accessing a remote COM object via C#

I have an exe file written using unmanaged C++ which contains a COM object. 我有一个使用非托管C ++编写的exe文件,其中包含一个COM对象。 In order to access it from a managed C# application, I generated an interop assembly. 为了从托管C#应用程序访问它,我生成了一个互操作程序集。 I was able to use this with great success when both apps were running on the same PC. 当两个应用程序在同一台PC上运行时,我能够非常成功地使用它。

Now, I have a requirement to make my C# app access the COM object on a remote PC and the existing code gave me some problems. 现在,我要求让我的C#app访问远程PC上的COM对象,现有代码给了我一些问题。 I had to make some small changes eg 我不得不做一些小改动,例如

Type ReportItemSetup = Type.GetTypeFromProgID("ACME.REPORTITEMSETUP.1", remotePCName);
RpiSetup = (IReportItemSetup)Activator.CreateInstance(ReportItemSetup);

became 成为

Guid gris = new Guid("{147FF3D1-8A00-11F0-9A6C-0000C099D00B}");
Type ReportItemSetup = Type.GetTypeFromCLSID(gris, remotePCName, true);
RpiSetup = (IReportItemSetup)Activator.CreateInstance(ReportItemSetup);

This enabled me to get a bit further through the code but then I reached another problem. 这使我能够进一步了解代码,但后来又遇到了另一个问题。

I use : 我用 :

REPORTITEMSETUPClass rpis = new REPORTITEMSETUPClass();

where REPORTITEMSETUPClass is (edited for brevity) 其中REPORTITEMSETUPClass(为简洁起见而编辑)

namespace Acme.ReportItemServers.Interop
{
    [ClassInterface(ClassInterfaceType.None)]
    [TypeLibType(TypeLibTypeFlags.FAppObject | TypeLibTypeFlags.FCanCreate | TypeLibTypeFlags.FPreDeclId)]
    [ComConversionLoss]
    [Guid("147FF3D1-8A00-11F0-9A6C-0000C099D00B")]
    public class REPORTITEMSETUPClass : IReportItemSetup, REPORTITEMSETUP, INotifySrc
    {
        public REPORTITEMSETUPClass();

        ... snip ...
        public virtual void INotifySrc_AddUser(INotify pNotify, string bstrUserName);
        ... snip ...
    }
}

I need to make a call to AddUser on the INotifySrc interface but the new call gives me the error : 我需要在INotifySrc接口上调用AddUser,但调用给出了错误:

Retrieving the COM class factory for component with CLSID {147FF3D1-8A00-11F0-9A6C-0000C099D00B} failed due to the following error: 
80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

This error is perfectly correct since it is not registered on the local machine. 此错误完全正确,因为它未在本地计算机上注册。

My question is therefore : is it not possible to use the registration on the remote PC? 因此,我的问题是:是否无法在远程PC上使用注册? The Activator.CreateInstance had no problems with the class not being registered locally. Activator.CreateInstance没有在本地注册的类没有问题。

I think you must write a serviced component to do this. 我认为你必须写一个服务组件来做到这一点。 This involves some COM+ magic and can be quite complex. 这涉及一些COM +魔法,可能非常复杂。 Look here for a summary: http://msdn.microsoft.com/en-us/library/3x7357ez%28v=vs.71%29.aspx 在这里查看摘要: http//msdn.microsoft.com/en-us/library/3x7357ez%28v=vs.71%29.aspx

创建System.EnterpriseServices.ServicedComponent的子类,然后将其安装在服务器上的COM +中,然后从服务器导出COM +代理(msi),并在客户端上安装此代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM