Activator.CreateInstance() works inside VSIDE but not externally

前端 未结 7 1504
天涯浪人
天涯浪人 2020-12-19 07:50

I have a bunch of COM objects which all implement the same interface, and need to create one of them as chosen at runtime from a list of options. Since I know the CLSID for

7条回答
  •  温柔的废话
    2020-12-19 08:38

    Two suggestions.

    Use [STAThread] attribute.

    [STAThread]
    static void Main(string[] args)
    {...}
    

    Try calling CoInitialize

    [DllImport("ole32.dll")]
    static extern int CoInitialize(IntPtr pvRes);
    CoInitialize((System.IntPtr)null)
    

提交回复
热议问题