COM引用问题:System.Runtime.InteropServices.COMException:使用CLSID创建COM组件的实例

时间:2014-06-13 13:22:31

标签: c# com vb6 msbuild mstest

我是一名法国实习生,我遇到了一个非常大的问题,所以请你帮帮我吧。

我的项目的目标是在C#程序中使用VB6编码的.dll自动化一些测试。 事实上,我使用VB项目中的一些类,这将允许我们防止代码中的回归。 .dll是x86所以我的C#项目。

以下是我如何使用dll

的示例
   using E2S_Equipment;
         …
         public void verifyEquipmentTextProperty(string eqpCode, bool equipmentIsDynamic, string textPropertyCode, bool textPropertyIsDynamic, string propValue)
        {
           //Class from E2S_Equipment dll
           claEQPSRVReadString readStrService = new claEQPSRVReadString();
           readStrService.LoadByKey(eqpCode);
           …  
        }

当我在Visual Studio中启动它们时,我的所有测试都取得了成功,但是当我使用带有MSTest的命令行启动它们时,第一次测试成功,其他测试失败。我的错误是:

  

System.Runtime.InteropServices.COMException:由于以下错误,从IClassFactory创建具有CLSID {987C190C-8CFD-4E41-882B-3BAE73768066}的COM组件实例失败:800a005b来自HRESULT的异常:0x800A005B。

我的问题涉及claEQPSRVReadString

的声明

我的第一个想法是我为每个测试创建的代码是一个新的COM对象实例,因此我创建了一个Singleton模式,只有一个我的COM组件实例:

public static class SrvReadTextPropertySingleton
{
    private static claEQPSRVReadString mEqpSrvReadTextProperty;
    public static claEQPSRVReadString EqpSrvReadTextProperty
    {
        get
        {
            if (mEqpSrvReadTextProperty == null)
            {
                mEqpSrvReadTextProperty = new claEQPSRVReadString();
            }
            return mEqpSrvReadTextProperty;
        }
    }
}

现在我收到此错误SrvReadTextPropertySingleton. EqpSrvReadTextProperty.LoadByKey(eqpCode);

  

System.Runtime.InteropServices.InvalidComObjectException:无法使用已与其基础RCW分离的COM对象。

那么,你有什么想法吗? 提前谢谢你!

2 个答案:

答案 0 :(得分:1)

您是否100%确定您的项目设置是否正确。您的平台目标应为x86,并且应勾选首选项32位复选框。您还从命令行运行调试版本或发行版本。您需要为这两种环境设置32位设置。 对于命令行版本,请打印以下内容以确保您的环境正确。

Console.WriteLine("OS {0}, Process {1}", System.Environment.Is64BitOperatingSystem, System.Environment.Is64BitProcess);

保持对正在使用的对象的引用以便在使用它之前创建它然后分配它的输出值也可能很有用。

var instance = new claEQPSRVReadString();
mEqpSrvReadTextProperty = instance.Value;

当然可以访问从实例读取的值。看起来它正在VB6组件的构建阶段完成所有工作,这是一种不寻常的方式。您是否在要创建的实例上使用任何其他方法。

答案 1 :(得分:0)

我发现了问题。它已经到了线程执行的链接。

在.testsettings文件中我们添加:

<Execution> 

  <ExecutionThread apartmentState="MTA"/>   

</Execution>

</TestSettings>

http://ralessi.wordpress.com/2013/09/11/mta-testing-on-vs2012/