我试图从 .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窗体应用程序可以在没有异常的情况下创建实例。
这里可能出了什么问题?
在这两种情况下,我都将[STAThread]属性应用于“Program”类。