简体   繁体   English

如何从COM对象调用方法(无法将System .__ ComObject强制转换为我的COM类)

[英]How to call methods from COM object (cannot cast System.__ComObject to my COM class)

I can create COM object locally 我可以在本地创建COM对象

var infsrv = new InfoServ.TInfoServerClass(); //COM object
infsrv.RunBsScript(bssScriptName, strOfParam); //calling method

But I needed to create COM object on server, so code is: 但是我需要在服务器上创建COM对象,因此代码是:

var myGuid = new Guid("00C4261D-0B2B-4230-A2CA-A9F4F2A46452");
var myType = Type.GetTypeFromCLSID(myGuid, servername);
InfoServ.TInfoServerClass infsrv = (InfoServ.TInfoServerClass)Activator.CreateInstance(myType);
infsrv.RunBsScript(bssScriptName, strOfParam);

but now it says: 但现在它说:

Unable to cast COM object of type 'System.__ComObject' to class type 'InfoServ.TInfoServerClass'. 无法将类型为“ System .__ ComObject”的COM对象转换为类类型为“ InfoServ.TInfoServerClass”。 COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the __ComObject type. 进入CLR且不支持IProvideClassInfo或未注册任何互操作程序集的COM组件将被包装为__ComObject类型。 Instances of this type cannot be cast to any other class; 此类型的实例不能转换为任何其他类; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface. 但是,只要基础COM组件支持对接口IID的QueryInterface调用,就可以将它们强制转换为接口。

what should I do? 我该怎么办?

You should be able to cast the instance created by the call to Activator.CreateInstance to an interface. 您应该能够将通过对Activator.CreateInstance的调用创建的实例转换为接口。

You should have an interface defined in the Interop assembly named InfoServ.TInfoServer (or ITInfoServer if the type library explicitly defined an interface for it), and if you cast the object returned by Activator.CreateInstance to that interface, you should see all of the methods/properties the CoClass (TInfoServerClass) exposes. 您应该在Interop程序集中定义一个名为InfoServ.TInfoServer的接口(如果类型库为它明确定义了一个接口,则应该为ITInfoServer),并且如果将Activator.CreateInstance返回的对象转换为该接口,则应该看到所有CoClass(TInfoServerClass)公开的方法/属性。

Usually you use the "Add Reference" dialog to add a reference to a COM library. 通常,您使用“添加引用”对话框来添加对COM库的引用。 Then you can use the imported types like you would use any other class. 然后,您可以像使用其他任何类一样使用导入的类型。

暂无
暂无

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

相关问题 :'无法将类型为'System .__ ComObject'的COM对象转换为接口类型 - : 'Unable to cast COM object of type 'System.__ComObject' to interface type 无法使用 STAThread 属性转换类型为“System.__ComObject”的 COM 对象 - Unable to cast COM object of type 'System.__ComObject' with STAThread attribute 无法转换类型为'System .__ ComObject'的COM对象 - Unable to cast COM object of type 'System.__ComObject' 无法转换'System .__ ComObject类型的COM对象 - Unable to cast COM object of type 'System.__ComObject 无法将类型为“ System .__ ComObject”的COM对象转换为类类型AgentInfo - Unable to cast COM object of type 'System.__ComObject' to class type AgentInfo 获取 CLR 对象属性引发无法转换类型为“System.__ComObject”的 COM 对象 - Getting CLR object property raises Unable to cast COM object of type 'System.__ComObject' 无法将类型'System .__ ComObject'的COM对象转换为IRTDUpdateEvent的接口类型 - Unable to cast COM object of type 'System.__ComObject' to interface type for IRTDUpdateEvent SAP:无法将“System.__ComObject”类型的 COM 对象转换为接口类型“sapfewse.GuiTextField” - SAP: Unable to cast COM object of type 'System.__ComObject' to interface type 'sapfewse.GuiTextField' 在C#中加载COM对象抛出异常“无法将类型为'System .__ ComObject'的COM对象转换为接口类型...”,但C ++或VB没有 - Loading COM object in C# throws exception “Unable to cast COM object of type 'System.__ComObject' to interface type …”, but C++ or VB not System.InvalidCastException:无法将类型为“System .__ ComObject”的COM对象强制转换为接口类型“System.Collections.IEnumerable” - System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM