【发布时间】:2012-01-11 22:39:54
【问题描述】:
我在使用 C# 中的 COM 类时遇到问题。 COM 类是用 C++ ATL 32 位开发的。
当我在 VBA、VB6、C++、Javascript 甚至 MSTest/C# 中使用 COM 类时,它可以正常工作
奇怪的是,当我从 NUnit 测试或控制台应用程序创建实例时,它会失败并出现异常:
System.InvalidCastException : Unable to cast COM object of type 'PvtsFlashLib.FlashClass' to interface type 'PvtsFlashLib.IFlash4'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{07065455-85CD-42C5-94FE-DDDC1B1A110F}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
有人能指出正确的方向吗?
提前感谢您的帮助。
测试项目和控制台项目的构建配置设置为:
Platform = x86
两个项目中的 COM 引用设置为:
Copy Local = True
Embed Interop Types = False
Isolated = False
运行良好的 MSTest 代码:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PvtsFlashLib;
namespace TestProject1
{
[TestClass]
public class TestOfComMSTest
{
[TestMethod]
public void CreateFlash()
{
var flash = new Flash();
flash.AdvancedOptions = PvtsFlashAdvancedOptionsEnum.AllProperties;
}
}
}
NUnit 测试失败的代码:
using NUnit.Framework;
using PvtsFlashLib;
namespace Test
{
[TestFixture]
public class TestOfComNUnit
{
[Test]
public void CreateFlash()
{
var flash = new Flash();
flash.AdvancedOptions = PvtsFlashAdvancedOptionsEnum.AllProperties;
}
}
}
同样失败的控制台应用代码:
using PvtsFlashLib;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
var flash = new Flash();
flash.AdvancedOptions = PvtsFlashAdvancedOptionsEnum.AllProperties;
}
}
}
我没有足够的声望点来回答我自己的问题。但她还是这样:
由于某种原因,无法从 MTAThread 创建 COM 对象。 MSTest 默认为 STAThread 和 NUnit,Console 默认为 MTAThread。将 [STAThread] 属性应用于 ConsoleTest.Main() 和 Test.CreateFlash() 即可解决问题。
【问题讨论】:
标签: com com-interop