无法转换类型为exception的COM对象 [英] Unable to cast COM object of type exception

查看:456
本文介绍了无法转换类型为exception的COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  public void Test(IMyInterface iInterface)
{
iInterface .CallMethod();
}

但是,如果我改变代码线程:

  private IMyInterface myInterface; 
public void Test(IMyInterface iInterface)
{
myInterface = iInterface;
new Thread(new ThreadStart(CallInterfaceMethod)).Start();
}

public void CallInterfaceMethod()
{
myInterface.CallMethod()
}
pre>

当我使用线程时,我收到异常:



无法转换类型System .__ ComObject'到接口类型'IMyInterface'。
此操作失败,因为COM组件的QueryInterface调用与IID'{GUID}'的接口失败,由于以下错误:没有这样的接口支持



但是接口应该支持只是罚款?

解决方案

这个令人讨厌的讨厌的异常是由于一个概念被称为COM编组。问题的本质在于,为了从任何线程消费COM对象,线程必须能够访问描述COM对象的类型信息。



在您描述的方案中,在第二个线程上失败的原因是因为第二个线程没有接口的类型信息。



您可以尝试将以下代码添加到代码中:

  [ComImport] 
[Guid (23EB4AF8-BE9C-4b49-B3A4-24F4FF657B27)]
public interface IMyInterface
{
void CallMethod();
}

基本上,上面的声明指示.NET框架COM加载以加载类型信息



您还应该将COM对象的创建限制为单个线程(以防止线程编组)来帮助解决这个问题。



总而言之,这个错误围绕类型信息和线程编组。确保每个想要访问COM对象的线程都具有相关信息,可以从源线程中解组对象。



PS:这个问题在.NET 4.0中解决了使用称为类型等价的技术


I have the following code:

public void Test(IMyInterface iInterface)
{
  iInterface.CallMethod ( );
}

Which works fine. However, if I change the code to be threaded:

private IMyInterface myInterface;
public void Test(IMyInterface iInterface)
{
  myInterface = iInterface;
  new Thread ( new ThreadStart ( CallInterfaceMethod) ).Start ( );
}

public void CallInterfaceMethod ( )
{
  myInterface.CallMethod ( )
}

When i use the thread I receive the exception:

Unable to cast COM object of type 'System.__ComObject' to interface type 'IMyInterface'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{GUID}' failed due to the follow error: No such interface supported

But the interface should be supported just fine? Anyone have any thoughts on what is going on here?

解决方案

This nasty, nasty exception arises because of a concept known as COM marshalling. The essence of the problem lies in the fact that in order to consume COM objects from any thread, the thread must have access to the type information that describes the COM object.

In your scenario described, the reason it fails on the second thread is because the second thread does not have type information for the interface.

You could try adding the following to your code:

[ComImport]
[Guid("23EB4AF8-BE9C-4b49-B3A4-24F4FF657B27")]
public interface IMyInterface
{
    void CallMethod();
}

Basically the declaration above instructs the .NET framework COM loaded to load type information using traditional techniques from the registry and locate the associated type library and go from there.

You should also restrict the creation of the COM object to a single thread (to prevent thread marshalling) to help solve this issue.

To summarize, this error revolves around type information and thread marshalling. Make sure that each thread that wants to access the COM object has the relevant information to unmarshal the object from the source thread.

PS: This problem is solved in .NET 4.0 using a technique called "Type Equivalence"

这篇关于无法转换类型为exception的COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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