.net - 当 Windows 窗体应用程序正常时,无法从控制台应用程序创建 COM 对象

标签 .net com com-interop

我尝试从 .Net Framework 4.0 控制台和 Windows 窗体应用程序创建相同的 COM 对象实例。

 var t = Type.GetTypeFromProgID("MyProgID");
 dynamic req = Activator.CreateInstance(t);

虽然我的控制台应用程序告诉我:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM cla
ss factory for component with CLSID {CF2D8AF3-287C-4107-8790-D37F169E2475} faile
d due to the following error: 80040154 Class not registered (Exception from
HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Windows 窗体应用程序无异常(exception)地创建实例。

这里有什么问题?

在这两种情况下,我都将 [STAThread] 属性应用于“程序”类。

最佳答案

我发现了这个问题。

COM 对象仅适用于 x86 环境。
虽然我的 Windows 窗体应用程序仅针对 x86 构建,但我的控制台是“任何 CPU”,它在 x64 环境中提供错误。

在我更改控制台应用程序的平台目标后,一切正常。

关于.net - 当 Windows 窗体应用程序正常时,无法从控制台应用程序创建 COM 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5617409/

相关文章:

.net - 从 VB6 代码中释放 .NET 对象

.NET Core 2.1 - 如何创建COM对象并生成*.tlb文件

.net - MSBuild 4.0 在 XMLQuery 任务(来自 MSBuild 社区任务包)上失败,并出现错误 MSB4018

c# - 好的C#asp.net开源项目,用于代码研究

c++ - 作为通用指针类型传递的智能指针在传递给另一个函数之前是否需要释放?

com - 什么是COM?

C# 符号

c# - 将 List<> 转换为 Array - 我得到 "Attempted to access an element as a type incompatible with the array."

c#-4.0 - COM Interop 中动态代码的内存泄漏

c# - COMException (0x800401E3) : Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) with scheduled task