我有以下(非常简单)的程序:
static void Main(string[] args)
{
// The program has 'using AutConnListTypeLibrary' in the header
AutConnList connections = new AutConnList();
connections.Refresh();
Debug.Print(connections.Count.ToString());
}
它应该连接到所有打开的PCOMM会话,并返回打开会话的数量(只是为了开始某些事情)。但是,当我运行程序时,我在第一行代码中得到以下运行时错误:
无法转换类型为' System .__ ComObject'的COM对象。接口 键入' AutConnListTypeLibrary.AutConnList'。此操作失败 因为QueryInterface调用COM组件的接口 与IID' {3CB39CC1-6F18-11D0-910D-0004AC3617E1}'由于失败而失败 以下错误:不支持此类接口(来自HRESULT的异常: 0x80004002(E_NOINTERFACE))。
我错过了参考?或者我的代码有问题吗?如上所述,这是一个运行时错误。代码编译得很好。
答案 0 :(得分:0)
事实证明,这个问题有一个简单的解决方案(虽然理解起来并不是很简单)。只需将[STAThread]
属性添加到Main()
方法即可。我原帖的代码应如下所示:
[STAThread]
static void Main(string[] args)
{
// The program has 'using AutConnListTypeLibrary' in the header
AutConnList connections = new AutConnList();
connections.Refresh();
Debug.Print(connections.Count.ToString());
}