简体   繁体   English

尝试实例化位于可执行文件中的COM对象时,COM Interop:System.InvalidCastException

[英]COM Interop: System.InvalidCastException when trying to instantiate COM object located in an executable

Using COM interop, I have following exception in my C# client code when trying to instantiate a COM object coded in C++ compiled and running in a COM executable server: System.InvalidCastException: Unable to cast object of type 'System.__ComObject' to type "XXX". 使用COM互操作,尝试实例化以C ++编码并在COM可执行服务器中运行的COM对象时,我的C#客户端代码具有以下异常:System.InvalidCastException:无法将类型为'System .__ ComObject'的对象转换为类型“ XXX”。 (Where XXX was the class type used in my C# code for the COM object) (其中XXX是我的C#代码中用于COM对象的类类型)

The executable containing the COM object runs on the same machine as the client code. 包含COM对象的可执行文件与客户端代码在同一台计算机上运行。 I have verified that the COM object is registered properly in the windows registry. 我已验证COM对象已在Windows注册表中正确注册。

The instantiation fails not systematically in my project. 实例化在我的项目中没有系统地失败。 I need to execute at least 3 times to see the failure. 我至少需要执行3次才能看到失败。 I have the same issue during the tests running in our CI system. 在我们的CI系统中运行的测试期间,我遇到了同样的问题。

The problem is solved: 2 assemblies were used by my project that had references to the Interop dll used to instantiate the COM object. 问题已解决:我的项目使用2个程序集,这些程序集引用了用于实例化COM对象的Interop dll。 One of these 2 assemblies had “EmbedInteropTypes” set to true and the other one had the same property set to false. 这两个程序集中的一个将“ EmbedInteropTypes”设置为true,而另一个将相同的属性设置为false。 Putting “EmbedInteropTypes” to false for both fixed the problem. 将“ EmbedInteropTypes”都设置为false可以解决此问题。

I am still looking for good explanations on why this did not work. 我仍在寻找关于为什么不起作用的良好解释。 I have found other projects where similar issues happened it seems: https://github.com/yck1509/ConfuserEx/issues/302 我发现了发生类似问题的其他项目: https : //github.com/yck1509/ConfuserEx/issues/302

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

相关问题 使用COM接口时System.InvalidCastException - System.InvalidCastException when using a COM interface System.InvalidCastException 尝试在 C# 中构建 COM 客户端和 COM 服务器 - System.InvalidCastException trying to build a COM client and a COM server in C# 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' 对象到字节— System.InvalidCastException - Object to byte — System.InvalidCastException System.InvalidCastException:尝试在 C# 中使用实体框架将对象添加到 SQL 数据库时 - System.InvalidCastException: When trying to add an object to a SQL database using Entity Framework in C# System.InvalidCastException:'无法投射对象 - System.InvalidCastException: 'Unable to cast object System.InvalidCastException:无法转换类型的对象 - System.InvalidCastException: Unable to cast object of type System.InvalidCastException:对象必须实现IConvertible - System.InvalidCastException: Object must implement IConvertible IList对象创建中的System.InvalidCastException - System.InvalidCastException in IList object creation 使用datagridview时发生System.InvalidCastException - System.InvalidCastException when work with datagridview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM